Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Creating Your First Slice with createSlice | Creating and Managing State with Slices
Керування станом з Redux Toolkit у React

bookCreating Your First Slice with createSlice

Свайпніть щоб показати меню

A slice is created using the createSlice function. This is where you define your state and how it can change.

Creating a Slice

Create a new file, for example:

src/features/counter/counterSlice.js

Inside this file, define your slice:

import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  value: 0
};

const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment(state) {
      state.value += 1;
    },
    decrement(state) {
      state.value -= 1;
    }
  }
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;

You define a piece of state and the logic that updates it.

createSlice automatically creates:

  • Actions based on your reducers;
  • A reducer function for the store;
  • A structured way to manage this part of state.
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 3. Розділ 2
some-alt