Skip to content

Latest commit

 

History

History
32 lines (21 loc) · 1.73 KB

401-class29.md

File metadata and controls

32 lines (21 loc) · 1.73 KB

Class 401.29

Notes

Extracting State Logic into a Reducer

  1. What is the motivation for adding a reducer?

A reducer can simplify complex state logic that involves multiple sub-values, or when the next state depends on the previous one. It also helps in testing and isolates the state logic from the components.

  1. What are actions in the context of a reducer? How are they different than setting state directly?

Actions in a reducer are plain objects that represent an action to be performed on the state, and usually have a 'type' property. They are different from setting state directly because they describe the changes in an abstract way, without mutating the state directly.

  1. What common list operation is useReduce named for, and why?

useReduce is named after the reduce function in JavaScript, which is used to apply a function against an accumulator and each element in the array (from left to right) to reduce it to a single output value.

  1. When should you switch from useState to useReducer?

The switch from useState to useReducer should be done when you have complex state logic that involves multiple sub-values, or when the next state depends on the previous one.

Bookmark and Review

Keep these pages handy - they answer questions that show up regularly for this lab.

Readings

Things I want to learn more about

References

  • Google Bard and ChatGPT