Sobes.tech
Middle

Name the basic React hooks and what they are used for.

sobes.tech AI

Answer from AI

Basic React hooks and their purpose:

  • useState — allows adding state to functional components.

  • useEffect — manages side effects, such as data loading or subscriptions.

  • useContext — provides access to React context for passing data without props.

  • useReducer — an alternative to useState for complex state logic.

  • useRef — creates a mutable object that persists between renders, often used for DOM access.

Example of using useState and useEffect:

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

function Counter() {
  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>
  );
}
Name the basic React hooks and what they are used… - sobes.tech