From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Managing quiz state with useReducer

Managing quiz state with useReducer

In this session, we'll be building the core logic for our driving test app using React's Use Reducer hook. This powerful hook allows us to manage complex state changes in a clean and organized way. We'll cover tracking the current question, storing user answers, handling navigation, and more. Now I am importing the questions data. Here I am adding the quizReducer function. This function will handle all the state updates for our quiz. It will receive the current state and an action and return the new state. Now it is time to define the initial state of our quiz application. This is an object that holds all the information about the state of the quiz before anything has happened. Let's initialize the CurrentQuestion property within the InitialState object to 0. This means the quiz will start at the first question. I am now adding the Answers array to the InitialState. This array will store the user's answers to each question. Its length will match the total number of questions…

Contents