Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Slice | Redux in Practice
Introduction to Redux

Creating a Slice

For creating a slice, we first import the createSlice function from Redux Toolkit:

The createSlice function takes in an object having name, initialValue, and reducers keys. Following is the code from todosSlice, which we imported in the last chapter:

The name is supposed to be the feature's name and can be anything.

We provide an initialState to the slice so that it's initiated with this value. The initialState can be anything ranging from a simple to a complicated nested object.

The reducers key contains an object in which every key represents an action name. The values of these keys are functions that are supposed to contain logic for those actions. When the addTodo action is dispatched, it is handled by the corresponding inline function.

In the above case, the inline function for addTodo action has two arguments: state and action. state represents the slice's state. In contrast, action represents the action object and can be optionally omitted.

The action object contains the type key however, there is a key called payload, which may hold additional data if any has been passed.

In the Redux store, we looked at in the previous chapter, there was a Todo slice:

However, we didn’t know what data it would have initially. After looking at the initialValue of the todosSlice we can tell that the initial state of the store will be something like this:

What keys are usually passed into the arguments object in createSlice ?

Select a few correct answers

Everything was clear?

Section 3. Chapter 5
course content

Course Content

Introduction to Redux

Creating a Slice

For creating a slice, we first import the createSlice function from Redux Toolkit:

The createSlice function takes in an object having name, initialValue, and reducers keys. Following is the code from todosSlice, which we imported in the last chapter:

The name is supposed to be the feature's name and can be anything.

We provide an initialState to the slice so that it's initiated with this value. The initialState can be anything ranging from a simple to a complicated nested object.

The reducers key contains an object in which every key represents an action name. The values of these keys are functions that are supposed to contain logic for those actions. When the addTodo action is dispatched, it is handled by the corresponding inline function.

In the above case, the inline function for addTodo action has two arguments: state and action. state represents the slice's state. In contrast, action represents the action object and can be optionally omitted.

The action object contains the type key however, there is a key called payload, which may hold additional data if any has been passed.

In the Redux store, we looked at in the previous chapter, there was a Todo slice:

However, we didn’t know what data it would have initially. After looking at the initialValue of the todosSlice we can tell that the initial state of the store will be something like this:

What keys are usually passed into the arguments object in createSlice ?

Select a few correct answers

Everything was clear?

Section 3. Chapter 5
some-alt