Sobes.tech
Middle+

What React hooks do you know?

sobes.tech AI

Answer from AI

Main React hooks that are frequently used:

  • useState — for managing local component state.
  • useEffect — for side effects, such as data fetching or subscriptions.
  • useContext — for accessing React context.
  • useReducer — for managing complex state with a reducer.
  • useCallback — for memoizing functions to prevent unnecessary re-renders.
  • useMemo — for memoizing computed values.
  • useRef — for storing mutable values that do not cause re-rendering.
  • useLayoutEffect — similar to useEffect, but called synchronously after all DOM mutations.
  • useImperativeHandle — for customizing the instance value that is exposed to parent components via ref.

Example of using useState and useEffect:

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

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

  useEffect(() => {
    document.title = `Counter: ${count}`;
  }, [count]);

  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}