Creating Your First Slice with createSlice
Glissez pour afficher le menu
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.
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 2
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Section 3. Chapitre 2