Redux in React.js — Reducers and Slices

Justin Langlinais
Oct 26, 2020 · 3 min read
Image for post
Image for post
Photo by Ivan Torres on Unsplash

Let’s explore a basic counter app. We’ll have a button to increment a counter, a button to decrement it, a form input to enter any integer, and a button to add that value to the counter. We’ll focus and highlight the structure of the app as we work through this project, to get a handle on an efficient way to connect all of the pieces.

Let’s start by using create-react-app and we will use the Redux template, which will set up our simple counter project for us.

npx create-react-app learning-react-the-basics --template redux

Take some time to look through the folder structure of the app. Some important files are app/store.js, features/counter/counterSlice.js, and features/counter/Counter.js.

// ...src/app/store.jsimport { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
export default configureStore({
reducer: {
counter: counterReducer,
},
});

The configureStore function, which is provided to us through Redux Toolkit, takes in a reducer object as an argument. Right now, it’s using counterReducer. Let’s take a look at what that is:

// .../src/features/counter/counterSlice.jsimport { createSlice } from '@reduxjs/toolkit';export const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: state => {
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
export const { increment, decrement, incrementByAmount } = counterSlice.actions;export default counterSlice.reducer;

Above, first of all, we will ignore the thunk and selector functions at the bottom of our file.

Now, this file’s default export is the reducer function for our counter logic. This is exactly what we’re importing in our store.js function. Let’s go back and take a closer look:

// ...src/app/store.jsexport default configureStore({
reducer: {
counter: counterReducer,
},
});

Above, when we configure our store, we’re passing an object, {counter: counterReducer}, to be the reducer. This object says that we want the counter section of our global Redux state (state.counter), and we want the counterReducer to be in control of how it gets updated when we dispatch an action to it.

Okay, makes sense. Now, looking back at our counterSlice.js file, I’ve got a question: What the H is a “slice”? A Redux Slice is a collection of reducer logic and actions for a single feature of our app. The name “slice” comes from the idea that we’re splitting up the root Redux state object into multiple “slices” of slate. Ever heard of separation of concerns? Holy…

As an example, in our jiu-jitsu class management app, the store setup might be something like this:

import { configureStore } from '@reduxjs/toolkit'
import usersReducer from '../features/users/usersSlice'
import submissionsReducer from '../features/submissions/submissionsSlice'
import topicsReducer from '../features/topics/topicsSlice'
export default configureStore({
reducer: {
users: usersReducer,
submissions: submissionsReducer,
topics: topicsReducer
}
});

Here, state.users, state.submissions, and state.topics are each a separate slice of the Redux state. since usersReducer is only responsible for handling the state.users slice, we can refer to it as our “slice reducer” function.

Hopefully this gets you started on exploring and understanding Reducers and slices in Redux!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store