Loading tutorials
useState is the right default for most state in a React component. This chapter shows where it stops being comfortable, so that the reducer pattern in the next chapters solves a problem you can already see. You will not write a reducer yet. The goal is to recognize the warning signs in code you al6 min readuseState hook. This chapter covers only the reducer function. Later chapters cover action design, immutable updates, purity rules, and the `useReduc5 min readswitch on action.type. It also assumes you know basic useState, JSX event handlers, and the object spread operator. Here the focus is the action: what it contains, how to name it, and what to leave out of it.6 min readuseReducer (covered in the previous chapters). It focuses on one rule: a reducer never changes the state it receives. It builds and returns a new value instead.5 min readuseReducer in a component and how dispatch connects event handlers to the reducer. It assumes you can write a reducer function that takes (state, action) and returns the next state, and that you know how action objects carry a type and extra fields. It also ass6 min readswitch on action.type, dispatch actions from event handlers, and copy objects and arrays with spread, map, and filter. Here you will learn what "pure" means for a reducer, why React checks it for you in development, and how to fix t6 min readuseState and useReducer for a given piece of state. It assumes you can write a reducer and call useReducer, and that you know useState, immutable updates with spread, and basic event handling. It doesn't re-teach those.5 min readuseReducer, the init function. It assumes you can write a reducer, dispatch actions, and read a basic useReducer component. It also assumes you know the useState lazy initializer form, useState(() => compute()). The chapter explains what init doe6 min readuseEffect and its cleanup function. It also assumes you know what fetch returns.6 min readdispatch that decide whether your component renders when and5 min readuseReducer, and dispatch action objects. It also assumes you know what createContext and useContext do in basic form. It covers sharing a reducer's state and dispatch function with a whole component subtree, without passing props through eve5 min readdescribe, it, expect). The examples use Vitest, and the same code works in Jest if you replace the import line.5 min readdispatch calls, and warns you when you forget to handle an action. It assumes you can write a reducer and call useReducer, and that you know basic TypeScript: type aliases, union 5 min read