Summarize the five principles for structuring state.
The five principles are: 1) only include minimal representation of the state, 2) compute everything else on demand, 3) let components own their state, 4) keep state as normalized as possible, and 5) don’t put derived state in the state. For example, instead of storing an array length, calculate it each time from the array itself.
Passing State Deeply with Context
What problem do Contexts aim to solve?
Contexts aim to solve the problem of “prop drilling” where you have to pass data through many layers of components. For example, if you have a theme that needs to reach many components, you can use context instead of passing the theme down each level.
What is one technique to try before useContext?
One technique to try before using useContext is component composition. This involves passing components as props or using children props. For example, instead of passing data, you may pass a
<Header />
component with its own props
What hook complements useContext for complex applications?
The
useReducer
hook complementsuseContext
for complex applications. When state logic is complex,useReducer
can manage it in a more predictable way thanuseState
. For example, if you have a shopping cart with many possible actions (adding, removing items, etc),useReducer
is a good choice.