Defination of redux avatar
a month ago
3.9 kB
Define state, actions, and reducers using createSlice in react definne with examples

Here's a detailed explanation of state, actions, and reducers using createSlice in React, along with examples:

What is createSlice?

createSlice is a utility function from the redux-toolkit library that helps you create a Redux slice, which is a self-contained piece of state and behavior. It's a convenient way to define a reducer, actions, and initial state for a specific part of your application.


In the context of createSlice, state refers to the initial data that is stored in the Redux store. This data can be an object, an array, or any other data structure. When you create a slice, you need to define the initial state for that slice.

Here's an example:

const initialState = {
  count: 0,
  name: 'John Doe'

In this example, the initial state is an object with two properties: count and name.


Actions are objects that describe what happened in your application. They are used to trigger changes to the state. In createSlice, you define actions as functions that return an action object.

Here's an example:

const increment = () => {
  return { type: 'increment' };

In this example, the increment action is a function that returns an action object with a type property set to 'increment'.


Reducers are functions that take the current state and an action, and return a new state. In createSlice, you define a reducer function that is used to update the state.

Here's an example:

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'increment':
      return { ...state, count: state.count + 1 };
      return state;

In this example, the reducer function takes the current state and an action as arguments. It checks the type property of the action and updates the state accordingly. If the action is an increment action, it increments the count property by 1. If the action is not recognized, it returns the current state unchanged.

Using createSlice

Now that we have defined the state, actions, and reducer, we can use createSlice to create a Redux slice:

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

const counterSlice = createSlice({
  name: 'counter',
  reducers: {
    increment: increment
  reducer: reducer

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

In this example, we create a counterSlice using createSlice. We pass the initial state, actions, and reducer functions as arguments. The createSlice function returns a counterSlice object that contains the actions and reducer.

Using the slice in your React application

To use the slice in your React application, you need to connect it to your Redux store. You can do this by using the configureStore function from redux-toolkit:

import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';

const store = configureStore({
  reducer: {
    counter: counterReducer

In this example, we create a Redux store using configureStore and pass the counterReducer as an argument. This sets up the store to use the counterSlice reducer.

You can then use the store in your React components to access the state and dispatch actions:

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment } from './counterSlice';

function Counter() {
  const count = useSelector((state) => state.counter.count);
  const dispatch = useDispatch();

  return (
      <p>Count: {count}</p>
      <button onClick={() => dispatch(increment())}>Increment</button>

In this example, we use the useSelector hook to access the count property of the state, and the useDispatch hook to dispatch the increment action. When the button is clicked, the increment action is dispatched, which updates the state and re-renders the component
Leave a Comment