- Does it remain unchanged over time? If so, it isn’t state.
- Is it passed in from a parent via props? If so, it isn’t state.
- Can you compute it based on existing state or props in your component? If so, it definitely isn’t state!
- Identify every component that renders something based on that state.
- Find their closest common parent component—a component above them all in the hierarchy.
- Decide where the state should live:
- Often, you can put the state directly into their common parent.
- You can also put the state into some component above their common parent.
- If you can’t find a component where it makes sense to own the state, create a new component solely for holding the state and add it somewhere in the hierarchy above the common parent component.
Local variables have limited scope.
Lifting the satate up to a common ancestor and pass it down as props to the components that need access.
Keep these pages handy - they answer questions that show up regularly for this lab.
Passing Props to a Component Rendering Lists State as Snapshot useState hook
I am excited for hooks, they would have been hugely helpful in our past final project but we weren’t allowed to use them, so I am eager to learn of their power!