@@ -6459,7 +6459,17 @@ Technically it is possible to write nested function components but it is not sug
64596459279. ### What is ` useReducer` ? Why do you use useReducer?
64606460 The ` useReducer` hook is a React hook used to manage **complex state logic** inside **functional components**. It is conceptually similar to **Redux**. i.e, Instead of directly updating state like with ` useState` , you **dispatch an action** to a **reducer function**, and the reducer returns the new state.
64616461
6462- The ` useReducer` hook is used when:
6462+ The ` useReducer` hook takes three arguments:
6463+
6464+ ` ` ` js
6465+ const [state , dispatch ] = useReducer (reducer, initialState, initFunction);
6466+ ` ` `
6467+
6468+ * ` ** reducer** ` : A function ` (state , action ) => newState` that handles how state should change based on the action.
6469+ * ` ** initialState** ` : The starting state.
6470+ * ` ** dispatch** ` : A function you call to trigger an update by passing an action.
6471+
6472+ The ` useReducer` hook is used when:
64636473
64646474 * The **state is complex**, such as nested structures or multiple related values.
64656475 * State updates depend on the **previous state** and **logic**.
@@ -6473,6 +6483,74 @@ Technically it is possible to write nested function components but it is not sug
64736483
64746484**[⬆ Back to Top](#table-of-contents)**
64756485
6486+ 280. ### How does ` useReducer` works? Explain with an example
6487+ The ` useReducer` hooks works similarly to Redux, where:
6488+
6489+ * You define a **reducer function** to handle state transitions.
6490+ * You dispatch actions to update the state.
6491+
6492+ **Counter Example with Increment, Decrement, and Reset:**
6493+ 1. Reducer function:
6494+
6495+ Define a counter reducer function that takes the current state and an action object with a type, and returns a new state based on that type.
6496+
6497+ ` ` ` js
6498+ function counterReducer (state , action ) {
6499+ switch (action .type ) {
6500+ case ' increment' :
6501+ return { count: state .count + 1 };
6502+ case ' decrement' :
6503+ return { count: state .count - 1 };
6504+ case ' reset' :
6505+ return { count: 0 };
6506+ default :
6507+ return state;
6508+ }
6509+ }
6510+ ` ` `
6511+ 2. Using ` useReducer` :
6512+ Invoke ` useReducer` with above reducer function along with initial state. Thereafter, you can attach dispatch actions for respective button handlers.
6513+ ` ` ` js
6514+ import React , { useReducer } from ' react' ;
6515+
6516+ function Counter () {
6517+ const initialState = { count: 0 };
6518+ const [state , dispatch ] = useReducer (counterReducer, initialState);
6519+
6520+ return (
6521+ < div style= {{ textAlign: ' center' }}>
6522+ < h2> Count: {state .count }< / h2>
6523+ < button onClick= {() => dispatch ({ type: ' increment' })}> Increment< / button>
6524+ < button onClick= {() => dispatch ({ type: ' decrement' })}> Decrement< / button>
6525+ < button onClick= {() => dispatch ({ type: ' reset' })}> Reset< / button>
6526+ < / div>
6527+ );
6528+ }
6529+
6530+ export default Counter ;
6531+ ` ` `
6532+ Once the new state has been returned, React re-renders the component with the updated ` state .count ` .
6533+
6534+ **[⬆ Back to Top](#table-of-contents)**
6535+
6536+ 281. ### Can you combine **useReducer** with **useContext**?
6537+
6538+ Yes, it's common to **combine** ` ** useReducer** ` **with** ` ** useContext** ` to build a lightweight state management system similar to Redux:
6539+
6540+ ` ` ` js
6541+ const AppContext = React .createContext ();
6542+
6543+ function AppProvider ({ children }) {
6544+ const [state , dispatch ] = useReducer (reducer, initialState);
6545+ return (
6546+ < AppContext .Provider value= {{ state, dispatch }}>
6547+ {children}
6548+ < / AppContext .Provider >
6549+ );
6550+ }
6551+ ` ` `
6552+
6553+ **[⬆ Back to Top](#table-of-contents)**
64766554## Old Q&A
64776555
647865561. ### Why should we not update the state directly?
0 commit comments