Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Reading State with useSelector | Reading and Updating State in Components
Tilstandshåndtering med Redux Toolkit i React

bookReading State with useSelector

Sveip for å vise menyen

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.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 4. Kapittel 1
some-alt