Loading tutorials
useState is the right tool for most state. This chapter shows the specific situations where it becomes hard to maintain. Those situations are the reason useReducer exists.3 min readswitch statements. It also assumes you have used useState in a React component. You won't call useReducer yet. First you will write the function it depends on.2 min readuseState. It also assumes you have seen a reducer's shape, (state, action) => nextState. Here the focus is the second argument: the action.3 min readmap and filter, and the reducer shape (state, action) => nextState from the previous chapters. It covers how to write the "next state" part without changing the old state.2 min readuseState. Earlier chapters covered the reducer function, action objects, and immutable updates. Here you connect them to a component.2 min readuseReducer, and that you know why state updates must not mutate objects. It explains what "pure" means for a reducer, why React checks it, and the mistakes that break it.3 min readuseState, and that you know how a reducer takes (state, action) and returns the next state. It also assumes you can write immutable updates with spread, map, and filter. Here you decide which hook fits a given piece of state.2 min readuseReducer in a component. It covers the third argument to useReducer, the init function, and the mistakes that come with it.2 min readfetch, React function components, useEffect, and the basics of useReducer and dispatch from earlier chapters. You will model a network request as a small state machine and keep the reducer pure.3 min readuseReducer, and dispatch actions from event handlers. It covers three behaviors of dispatch that affect how you write handlers and effects.3 min readuseReducer, and use useContext at a basic level. It also assumes you know that dispatch keeps the same identity between renders.2 min readswitch on action.type, that you know why reducers must be pure and update state immutably, and that you have a JavaScript test runner such as Vitest set up. The examples use Vitest. Jest uses the same describe, it, and expect calls.3 min readuseReducer, actions, and reducers work, and that you have basic TypeScript experience with type aliases and unions. You will type a reducer so the compiler checks every action you dispatch.3 min read