====== React Hooks ====== React Hooks revolutionized the way developers build React applications by allowing functional components to manage state and side effects. This guide explores advanced hooks and their practical applications. **Understanding Hooks** Hooks enable function components to use React features without writing a class. Advanced hooks help in optimizing performance, managing complex state logic, and handling side effects efficiently. ==== 1. useReducer: Managing Complex State ==== ''useReducer'' is an alternative to ''useState'' for managing complex state logic. **Example:** import { useReducer } from 'react'; const initialState = { count: 0 }; function reducer(state, action) { switch (action.type) { case 'increment': return { count: state.count + 1 }; case 'decrement': return { count: state.count - 1 }; default: throw new Error(); } } function Counter() { const [state, dispatch] = useReducer(reducer, initialState); return (

Count: {state.count}

); } export default Counter;
==== 2. useContext: Managing Global State ==== ''useContext'' allows sharing state between components without prop drilling. Example: import { createContext, useContext, useState } from 'react'; const ThemeContext = createContext(); function ThemeProvider({ children }) { const [theme, setTheme] = useState('light'); return ( {children} ); } function ThemedComponent() { const { theme, setTheme } = useContext(ThemeContext); return (

Current Theme: {theme}

); }
==== 3. useMemo: Performance Optimization ==== ''useMemo'' memoizes expensive calculations. Example: import { useMemo, useState } from 'react'; function ExpensiveCalculation({ num }) { const squared = useMemo(() => { console.log('Calculating Square'); return num * num; }, [num]); return

Squared Value: {squared}

; }
==== 4. useCallback: Optimizing Function References ==== ''useCallback'' memoizes function instances. **Example:** import { useCallback, useState } from 'react'; function Button({ onClick }) { return ; } function ParentComponent() { const [count, setCount] = useState(0); const increment = useCallback(() => setCount(count + 1), [count]); return (

Count: {count}

); }
==== 5. useRef: Handling DOM and Persistent Values ==== ''useRef'' stores values without causing re-renders. **Example:** import { useRef } from 'react'; function InputFocus() { const inputRef = useRef(null); return (
); }
==== 6. useLayoutEffect: Synchronous Effects ==== ''useLayoutEffect'' runs synchronously after DOM mutations. **Example:** import { useLayoutEffect, useRef } from 'react'; function LayoutEffectComponent() { const divRef = useRef(null); useLayoutEffect(() => { console.log(divRef.current.offsetWidth); }); return
Hello
; }