Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Writing Reducers the Simple Way | Creating and Managing State with Slices
Tillståndshantering med Redux Toolkit i React

bookWriting Reducers the Simple Way

Svep för att visa menyn

Reducers define how state changes. In Redux Toolkit, reducers are written in a much simpler way compared to older Redux.

Writing Reducers

Inside a slice, reducers look like regular functions:

increment(state) {
  state.value += 1;
}

At first glance, it looks like you are modifying the state directly.

Why This Works

In standard Redux, you are not allowed to change state directly. You had to return a new object every time.

Redux Toolkit uses a tool under the hood that lets you write simpler code while still keeping state updates safe.

The Key Idea

You write reducers as if you are changing the state. Redux Toolkit takes care of turning those changes into safe updates.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 3. Kapitel 3
some-alt