Chapter 11. useReducer
In Chapter 4, State, you used the useState
hook to access a component’s state. In this chapter, you will explore another React hook: useReducer
, which lets you set, modify, and access a state variable within a component.
You will take advantage of useReducer
to add a shopping cart to Code Café and manage its ongoing value.
If you have previously used Redux, a third-party library for managing state, some of the patterns you see in this chapter might be familiar. However, you do not need any prior knowledge of or experience with Redux to use the useReducer
hook.
By the end of this chapter, users will be able to add items to a cart, and a new cart icon in the header will display the number of items in the cart (Figure 11.1, ...
Get React Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.