States and Effects
Resources
I. States
We often want our components to undergo visual changes as a result of user-computer interactions. To do this, a component needs to “remember” things about itself. This is what state is, a component’s memory.
0. Hooks
Hooks are functions that let you use React features. All hooks are recognizable by the use prefix. For example, useState is a hook.
1. Hooks can only be called from the top level of a functional component.
- Hooks are tied to a specific component instance — React tracks hook state by the order they're called per render of a specific component. Outside a function, there's no component instance to attach the state to.
- Outside the function = module scope — code there runs once when the file loads, not on each render. React wouldn't know which component owns that state or when to re-render it. → The hook must be defined within the component’s function