Sobes.tech
Junior — Middle

Can you name the main types of hooks in React or similar frameworks?.

sobes.tech AI

Answer from AI

In React and similar frameworks, the main types of hooks can be divided into several categories:

  • State hooks: allow managing the component's state. For example, useState in React.

  • Effect hooks: allow performing side effects, such as data loading or subscriptions. In React, this is useEffect.

  • Context hooks: allow accessing context, such as useContext.

  • Ref hooks: for accessing DOM elements or storing mutable values, such as useRef.

  • Custom hooks: user-defined functions that use basic hooks to reuse logic.

Example of using useState and useEffect in React:

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

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

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

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