From the course: React: State Management

Unlock the full course today

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

Moving logic into a function reducer

Moving logic into a function reducer - React.js Tutorial

From the course: React: State Management

Moving logic into a function reducer

- [Instructor] UseReducer takes a reducer function and one initial state as arguments. Then it returns an array with two elements, a state and a function dispatch. Right here, line 18. So the state represents the current state. Dispatch is a function that you use to send actions to the function reducer. So what is the function reducer exactly? The function reducer is where you handle all the state updates, based on actions that you dispatch from the components. And so the role of the reducer function is to return a new state. So what do we do in this part? In this part, we're going to complete the function reducer. We're going to add a switch statement to handle the different use cases, and then allow to return a new state that we're going to use to apply side effects inside the components. For the sake of good practices, and to keep things organized, I decided to move inside the components folder, the form components and results as well in their own file. So that's going to allow to…

Contents