5 React hooks every developer must know

5 React hooks every developer must know

Hooks, are probably the most important part of react. I am pretty sure you all must have used react hooks at some point in your development.

So here is the list of my top 5 most used react hooks.

useState()

useState is a hook used for managing the state in functional components. It allows you to declare a state variable and provides a function to update its value. The useState hook returns an array with two elements: the current state value and a function to update the state.

import React, { useState } from 'react';

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

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

useEffect()

useEffect is a hook used for performing side effects in functional components. It allows you to perform tasks such as data fetching, subscriptions, or manually updating the DOM after rendering. The useEffect hook takes a function as its first argument and an optional array of dependencies as its second argument.

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

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

  useEffect(() => {
    // Perform data fetching or any other side effect
    fetchData().then((result) => setData(result));
  }, []); // Empty dependency array, so it runs only once

  return <div>{data ? <p>{data}</p> : <p>Loading...</p>}</div>;
}

useContext()

useContext is a hook used for accessing the value of a React context in functional components. It allows you to consume context without wrapping your component in a context consumer. The useContext hook takes a context object as its argument and returns its current value.

import React, { useContext } from 'react';

const ThemeContext = React.createContext('light');

function ThemeComponent() {
  const theme = useContext(ThemeContext);

  return <p>Current theme: {theme}</p>;
}

useRef()

useRef is a hook used for creating a mutable ref object that persists across component renders. It allows you to keep a reference to a DOM element or any other value without triggering a re-render when its value changes. The useRef hook returns a mutable ref object with a .current property.

import React, { useRef } from 'react';

function InputComponent() {
  const inputRef = useRef();

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

  return (
    <div>
      <input type="text" ref={inputRef} />
      <button onClick={handleButtonClick}>Focus Input</button>
    </div>
  );
}

useCallback()

useCallback is a hook used for memorizing a function. It returns a memorized version of the callback function that only changes if one of the dependencies has changed. It is useful for optimizing performance by preventing unnecessary re-rendering of child components.

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

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

  const increment = useCallback(() => {
    setCount((prevCount) => prevCount + 1);
  }, []); // Empty dependency array, so it doesn't change

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

Well, there are more hooks that we all developers use, but according to me, these are the top 5 hooks we as developers use the most.

Thank you for reading.

Did you find this article valuable?

Support Manas Upadhyay by becoming a sponsor. Any amount is appreciated!