DEV Community

Cover image for Mastering React Hooks: Simplifying Your Components
Md Mafiz Uddin
Md Mafiz Uddin

Posted on

Mastering React Hooks: Simplifying Your Components

React Hooks

React Hooks are a way to add state and other React features to functional components. They were introduced in React 16.8 and are a simpler way to manage state and component lifecycle methods in your components, compared to class components.

Popular React Hooks

Here are some popular React Hooks that we will be discussing in this blog:

  1. - useState
  2. - useEffect
  3. - useContext

Let's dive into each of these hooks and understand what they do, and how to use them in our components.

.
.
.

useState

The useState hook allows us to add state to our functional component. State is data that can change and affect our component's rendering. We use useState by passing in an initial value, and it returns an array with two items: the current state value and a function to update it.

import React, { useState } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

.
.
.

useEffect

The useEffecthook lets us run some code after every render of our component, and it can also control how often that code is run. This hook is great for making API calls, handling component cleanup, and more. We use useEffectby passing in a callback function that runs after every render of the component. We can also pass in a second argument, an array of values, to control how often the effect runs.

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `You clicked ${count} times`;
  }, [count]);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

.
.
.

useContext

The useContexthook allows us to easily access and use data from our React context in our functional component. This hook makes it easy to share data between components without passing props down the component tree. We use useContextby passing in the context we want to access and it returns the current value of that context.

import React, { useContext } from 'react';

const MyContext = React.createContext();

function Example() {
  const value = useContext(MyContext);

  return (
    <div>
      <p>The value from context is: {value}</p>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Important Points :

  • React Hooks allow you to add state and other React features to functional components.
  • Popular Hooks include useState, useEffect, useContext, useReducer, useRef, and useMemo.
  • Hooks allow you to reuse state logic across multiple components without having to write a class component.
  • Hooks can make your code cleaner, more organized, and easier to maintain.
  • You can use Hooks to manage complex state, perform data fetching, handle side-effects, and optimize performance.
  • React Hooks are a recent addition to the React library, but they have quickly become a popular way to write modern React applications. . . #### Now React Hooks are a powerful tool for adding state and other React features to functional components. Popular Hooks like useState, useEffect, useContext, useReducer, useRef, and useMemoprovide a more organized, maintainable, and reusable way of writing React applications. By allowing developers to manage complex state, perform data fetching, handle side-effects, and optimize performance, Hooks have quickly become a popular way to write modern React applications.

Before We End...

That's all for now. I hope you've found this helpful

Regards:
Md Mafizuddin

For any queries connect with me:
LinkedIn: @mafizonly
Twitter: @whyknowmee
Github: @pacehut
Instagram: @mafiz._

Top comments (2)

Collapse
 
naucode profile image
Al - Naucode

Hey, that was a nice read, you got my follow, keep writing 😉

Collapse
 
pacehut profile image
Md Mafiz Uddin

Thanks bud✨