Menu

React useReducer – Interview Questions & Answers
📰

React useReducer – Interview Questions & Answers

DEV Community: react·Sajin Joe J·about 4 hours ago
#nkrfCg9Y
#devtoreact#devto#dev#state#action
Long article (457 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.