React State Management Flashcards
What is state management?
A way of maintaining and sharing data across components.
What is the primary way of managing state (v16.8+)?
The React hook useState
.
E.g. const [name, setName] = useState('Anna')
What is the difference between local and global state?
- Local: state accessible by a single component
- Global: state accessible by any component within a component hierarchy
What is Redux Toolkit (RTK)?
A library specialised in global state management. RTK is the recommended way to implement Redux.
What is prop drilling?
When props are passed down through components that don’t need it until it reaches a child that does.
How can React manage state globally?
By using one of the following…
* the Context API
* an external library (e.g. Redux, MobX, Zustand)
What is an action?
An event that describes something that happened in the application. Implemeneted as a plain JavaScript object that has a type
field.
What is a reducer?
A function that changes state by reducing a collection of actions by type.
When would you use useReducer
(instead of useState
)?
When a component deals with complex state, usually an array or object
Explain React’s built-in global state management feature.
It’s called the Context API and it can be used via useContext
. First createContext
and put a Provider
at the top-level of the component hierarchy. Then access the context via useContext
at the component level.