Q1. What is useReducer in React?
useReducer is a React Hook used to manage state using a reducer function and actions.
It is useful when state logic becomes complex or when multiple actions update the same state.
const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
Enter fullscreen mode
Exit fullscreen mode
state – current state value.
dispatch – function used to send an action.
reducer – function that decides how state changes.
initialState – initial value of the state.
Q2. What is a reducer function?
A reducer is a function that takes two arguments:
Current state
Action It returns the updated state.
syntax:
function reducer ( state , action ) { return newState ; }
Enter fullscreen mode
Exit fullscreen mode
Example:
function reducer ( state , action ) { switch ( action . type ) { case "Increment" : return state + 1 ;
case "Decrement" : return state - 1 ;
case "Reset" : return 0 ;
default : return state ; } }
Enter fullscreen mode
Exit fullscreen mode
Q3.…