What are hooks in React and how to use them?
Hooks are functions that allow you to use state and other React features in functional components. Examples include useState
, useEffect
, useContext
, etc. For example, the useState
hook is used to handle local state:
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>
);
}