You clicked {count} times
Timer:{seconds} seconds
; } Here are some of the main React Hooks and their uses: **useState**β Used to add state to a function component; you can use it to track data that changes over time. const [state, setState] = useState(initialState); **useEffect**β Used to perform side effects, such as data fetching, subscriptions, or manual DOM manipulations. Itβs similar to the lifecycle methods `componentDidMount`, `componentDidUpdate`, and `componentWillUnmount` in class components. useEffect(() => { // Perform side effect operations return () => { // Cleanup operations };}, ); // Dependency array **useContext**β Used to access data passed through the React context in the component tree without having to pass props through each component. const value = useContext(MyContext); **useReducer**β Used for more complex state logic. It takes a reducer function and an initial state, then returns the current state and a dispatch function for dispatching actions. const [state, dispatch] = useReducer(reducer, initialState); **useCallback**β Used to return a memoized version of a callback function, preventing unnecessary re-renders. const memoizedCallback = useCallback( () => { // Callback function body }, // Dependency array); **useMemo**β Used to memoize computed results, avoiding repeated calculations on every render. const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]); **useRef**β Used to create references to DOM elements or values, allowing you to maintain state across renders. const refContainer = useRef(initialValue); **useImperativeHandle**β Used to expose methods of a DOM element when using refs. useImperativeHandle(ref, () => ({ // Exposed methods})); useLayoutEffectβ Similar to useEffect, but it runs synchronously after all DOM updates. This is very useful when you need to read the DOM layout and trigger re-renders synchronously. useLayoutEffect(() => { // Side effect operations}, ); **useDebugValue**β Used to display custom hook labels in React Developer Tools. useDebugValue(value); ### 3. Benefits of Using React Hooks * **Simpler Component Logic**: No need to write class componentsβfunction components and Hooks can be used to manage state and lifecycle. * **Improved Code Reusability**: Hooks help you extract logic into reusable functions, reducing duplicate code. * **Better Performance Optimization**: Using Hooks like `useEffect`, `useCallback`, and `useMemo` allows you to control side effects and performance consumption more precisely. ### 4. Things to Note * **Use Hooks Only at the Top Level**: Donβt call Hooks inside loops, conditions, or nested functions. Make sure Hooks are called in the same order on every render. * **Use the ESLint Plugin**: React provides the eslint-plugin-react-hooks plugin to help you check whether your Hook usage is correct. ### 5. Example Hereβs an example that uses multiple React Hooks: ## Example import React,{ useState, useEffect } from 'react'; function Example(){ const[count, setCount]= useState(0); useEffect(()=>{ document.title= `You clicked ${count} times`; },);// Update the title only when count changes return(You clicked {count} times
YouTip