Reading State with useSelector
Svep för att visa menyn
To use data from the store inside a component, you read it with useSelector.
This hook lets your component access the current state.
Reading State
Inside a component, call useSelector and return the part of the state you need:
import { useSelector } from 'react-redux';
function Counter() {
const value = useSelector((state) => state.counter.value);
return <p>{value}</p>;
}
The function receives the full store state, and you select only what you need.
Why This Matters
Components should not depend on the entire state. By selecting only a small part, you keep components simple and easier to maintain.
The component will update automatically when the selected state changes. You don't need to manually trigger updates.
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 1
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Avsnitt 4. Kapitel 1