DEV Community

Cover image for 10 Essential React Hooks You Should Know
Bhavesh Yadav
Bhavesh Yadav

Posted on

10 Essential React Hooks You Should Know

Introduction

As a developer working with React, understanding hooks is crucial to writing clean, maintainable, and reusable code. React hooks introduced a paradigm shift in building components by providing a way to use state and lifecycle features in functional components. In this blog, we will delve into the 10 most essential React hooks you should know. By mastering these hooks, you will be able to write more concise, efficient, and reusable code, making your React applications more robust and maintainable.

1. useState - Simple State Management

The useState hook allows you to manage state within functional components. It provides a more efficient alternative to using classes for state management. The useState hook takes an initial state as its argument and returns an array with two elements: the current state value and a function to update the state.

import React, { useState } from 'react';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

2. useEffect - Perform Side Effects

The useEffect hook allows you to perform side effects in functional components, such as fetching data, subscribing to a service, or manually changing the DOM. It takes two arguments: a function containing the side effect and an array of dependencies. The side effect function will run after every render unless you provide a dependency array, in which case it will only run when one of the dependencies changes.

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

function Example() {
  const [data, setData] = useState([]);

  useEffect(() => {
    // Fetch data from API
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data));
  }, []);

  return (
    <div>
      {data.map(item => (
        <p key={item.id}>{item.name}</p>
      ))}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

3. useContext - Access React Context

The useContext hook allows you to access the value of a React context. React context provides a way to pass data through the component tree without having to pass props down manually at every level. The useContext hook takes a context object as its argument and returns the current value of that context.

import React, { useContext } from 'react';
import MyContext from './MyContext';

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

  return <p>Value from context: {value}</p>;
}
Enter fullscreen mode Exit fullscreen mode

4. useReducer - Complex State Management

The useReducer hook is an alternative to useState for managing complex state logic. It is used when the state depends on the previous state or when there are complex state transitions. The useReducer hook takes a reducer function and an initial state as arguments and returns the current state and a dispatch function.

import React, { useReducer } from 'react';

const initialState = { count: 0 };

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      throw new Error('Invalid action');
  }
}

function Example() {
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <div>
      <p>Count: {state.count}</p>
      <button onClick={() => dispatch({ type: 'increment' })}>Increment</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>Decrement</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

5. useCallback - Memoize Callback Functions

The useCallback hook allows you to memoize a callback function, preventing it from being recreated on every render. It takes a callback function and an array of dependencies as arguments and returns a memoized version of the callback function that only changes if one of the dependencies changes.

import React, { useCallback } from 'react';

function Example() {
  const handleClick = useCallback(() => {
    // Handle click event
    // Some expensive computations or operations
  }, []);

  return <button onClick={handleClick}>Click Me</button>;
}
Enter fullscreen mode Exit fullscreen mode

6. useMemo - Memoize Values

The useMemo hook allows you to memoize a value, preventing it from being recomputed on every render. It takes a function that returns the value to be memoized and an array of dependencies as arguments and returns a memoized version of the value.

import React, { useMemo } from 'react';

function Example() {
  const expensiveValue = useMemo(() => {
    // Perform expensive computations
    // Return a value
  }, []);

  return <p>Expensive Value: {expensiveValue}</p>;
}
Enter fullscreen mode Exit fullscreen mode

7. useRef - Create Mutable References

The useRef hook allows you to create a reference to a mutable value. It is similar to using an instance variable in a class component. The useRef hook returns a mutable ref object whose current property is initialized to the passed argument.

import React, { useRef } from 'react';

function Example() {
  const inputRef = useRef(null);

  const handleClick = () => {
    inputRef.current.focus();
  };

  return (
    <div>
      <input ref={inputRef} type="text" />
      <button onClick={handleClick}>Focus Input</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

8. useImperativeHandle - Customize Instance Values

The useImperativeHandle hook allows you to customize the instance value that is exposed when a parent component uses ref to access the child component. It takes two arguments: a ref and a function that returns an object defining the instance value.

import React, { useImperativeHandle, useRef } from 'react';

const CustomInput = React.forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    },
    getValue: () => {
      return inputRef.current.value;
    }
  }));

  return <input ref={inputRef} type="text" />;
});

function Example() {
  const customInputRef = useRef();

  const handleButtonClick = () => {
    customInputRef.current.focus();
    const value = customInputRef.current.getValue();
    // Do something with the value
  };

  return (
    <div>
      <CustomInput ref={customInputRef} />
      <button onClick={handleButtonClick}>Submit</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

9. useLayoutEffect - Perform Synchronous Layout Effects

The useLayoutEffect hook is identical to useEffect, but it runs synchronously after all DOM mutations. It is useful for reading layout from the DOM and synchronously re-rendering.

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

function Example() {
  const [width, setWidth] = useState(0);

  useLayoutEffect(() => {
    function handleResize() {
      setWidth(window.innerWidth);
    }

    window.addEventListener('resize', handleResize);
    handleResize();

    return () => {
      window.removeEventListener('resize', handleResize);
    };
  }, []);

  return <p>Window Width: {width}</p>;
}
Enter fullscreen mode Exit fullscreen mode

10. useDebugValue - Display Label for Custom Hooks

The useDebugValue hook allows you to display a label for custom hooks in React DevTools. It is particularly useful for building shared libraries where it is important to identify custom hooks correctly.

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

function useCustomHook() {
  const [value, setValue] = useState('');

  useDebugValue(value ? 'Value is set' : 'Value is not set');

  return [value, setValue];
}

function Example() {
  const [value, setValue] = useCustomHook();

  return (
    <div>
      <input
        type="text"
        value={value}
        onChange={e => setValue(e.target.value)}
      />
      <p>Value: {value}</p>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this comprehensive guide, we have explored the 10 most essential React hooks: useState, useEffect, useContext, useReducer, useCallback, useMemo, useRef, useImperativeHandle, useLayoutEffect, and useDebugValue.

By mastering these hooks, you now have a powerful set of tools to write clean, efficient, and reusable React code. Whether you're managing state, performing side effects, accessing context, or optimizing performance, React hooks empower you to build robust and maintainable applications.

Now, go forth and leverage the full potential of React hooks in your projects!

By the way, I hope this comprehensive guide has been helpful and informative! Feel free to comment if you have any questions.

Happy coding! 🚀😊

Top comments (0)