Skip to content
theLoneCoder text logo

WHY HAVE FRIENDS WHEN YOU HAVE CODE

  • Home
  • JavaScript
    • React.js
    • Node.js
    • DOM
  • Other
    • Bash/Terminal
    • Resources and Reviews
  • About Me
  • Contact Me
  • Home
  • JavaScript
  • React.js
  • What Are React Hooks? | React Tutorial for Beginners
What are React hooks?

What Are React Hooks? | React Tutorial for Beginners

Posted on April 19, 2023April 20, 2023 By theLoneCoder 2 Comments on What Are React Hooks? | React Tutorial for Beginners
React.js

 

React hooks are a notoriously confusing part of using functional components. While class-based components have certain built-in features thanks to deriving from the React.Component class, functional components have to “hook into” these features.

KEEP UP WITH { theLoneCoder }
Thank you for subscribing.
Something went wrong.
I upload new articles and tutorials daily throughout the week, so stay updated! You'll receive new tutorials on React, Node, and more.
We hate spam. Your email address will not be sold or shared with anyone else.

In the previous article, we learned about props in React. In this guide, I’m going to explain what react hooks are, and how to use them in your functional components.

For the official explanation of hooks, you can check the React docs. But be aware that their explanation isn’t near as good as the one I’m about to give you (wink wink).

Prerequisite Knowledge

  • React functional components
  • (optional) React class components
  • React state

What Are React Hooks?

If you’re familiar with class components, then you know that features such as state and render effects are built into it. For state in class components, all you need to do is use this.state. To create a render effect, class components have lifecycle methods such as componentDidMount.

Functional components do not have these features built-in like class components do. This is because unlike classes, which can derive from React.Component, functional components cannot “derive” from anything. They inherit no functionality from the React library.

In a sense, React features such as state are features that we need to “hook into.” This concept is visualized below:

React hooks diagram
So how do we hook into these features in a functional component? We do it with — you guessed it — React hooks.

For example, useState, useEffect, and useContext. state, (render) effects, and context are all features of React that are not built into functional components.

How to Use React Hooks

It’s important to realize that a React hook is nothing but a JavaScript function.

Let’s look at useState for example.

To use React state in functional components, first import the useState hook:


import { useState } from 'react';

The useState hook accepts the initial state and returns an array containing the state variable and a function to set the state. Hooks are meant to be called inside of a functional component.


import { useState } from 'react';

const MyComponent = () => {
	const [state, setState] = useState({});
};

And just like that, we can use state in our functional component! This is essentially how all React hooks work: we import a function and call it, which gives us access to the “hooked” feature!

Since we explored the useState hook in greater detail in my article on state in functional components, in the next article we are going to look at useEffect, a hook that allows us to create render effects in React.

 

Check out these articles!

Post navigation

❮ Previous Post: React Props in Functional and Class Components
Next Post: The React useEffect Hook and How to Use it ❯

You may also like

Deprecation notice: ReactDOM.render is no longer supported in React 18. Use createRoot instead
React.js
[FIXED!] ReactDOM Deprecation notice | ReactDOM.render vs. createRoot
April 25, 2023
JavaScript
How to Render React Components Conditionally
January 18, 2023
React.js
Do I Still Need to Import React? React 17 Update Means You Don’t
January 13, 2023
MERN Stack vs MEAN Stack
JavaScript
MERN Stack vs. MEAN Stack: What’s the Difference?
January 24, 2023

2 thoughts on “What Are React Hooks? | React Tutorial for Beginners”

  1. Pingback: The React useEffect Hook and How to Use it - theLoneCoder
  2. Pingback: MERN Stack Roadmap: a Guide for Developers - theLoneCoder

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • YouTube
  • GitHub
  • CodePen
  • Twitter
Substack

Subscribe to theLoneCoder!

Get updates on all new tutorials and blog posts from theLoneCoder.

Thank you!

You have successfully joined our subscriber list.

Recent Posts

  • [FIXED!] apt upgrade: the following packages have been kept back
  • [FIXED!] ReactDOM Deprecation notice | ReactDOM.render vs. createRoot
  • How to Show and Hide React Components (Easy!)
  • How to Write Comments in React/JSX
  • The React useEffect Hook and How to Use it

Categories

  • Bash/Terminal
  • DOM
  • JavaScript
  • Node.js
  • Other
  • React.js
  • Resources and Reviews
  • Home
  • Privacy Policy
  • About Me
  • Contact Me
  • YouTube

Copyright © 2023 theLoneCoder.

Theme: Oceanly News Dark by ScriptsTown