What is @reduxjs/toolkit?
The @reduxjs/toolkit package is a toolset for efficient Redux development. It is intended to be the standard way to write Redux logic, providing utilities to simplify common tasks such as store setup, creating reducers and actions, and writing immutable update logic.
What are @reduxjs/toolkit's main functionalities?
Creating a Redux Store
This feature simplifies the setup of the Redux store with sensible defaults and middleware like Redux Thunk.
import { configureStore } from '@reduxjs/toolkit';
const store = configureStore({ reducer: rootReducer });
Creating Slices
Slices are a way to define reducers and associated actions together in a single, concise object.
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1,
decrement: state => state - 1
}
});
export const { increment, decrement } = counterSlice.actions;
Creating Async Thunks
Async thunks are used to handle asynchronous logic in Redux. They can be dispatched like regular actions and handle the lifecycle of an async request.
import { createAsyncThunk } from '@reduxjs/toolkit';
const fetchUserById = createAsyncThunk(
'users/fetchByIdStatus',
async (userId, thunkAPI) => {
const response = await fetch(`https://api.example.com/users/${userId}`);
return await response.json();
}
);
Immutable Update Logic
The toolkit enables writing reducers with simpler immutable update logic using the Immer library under the hood.
import { createReducer } from '@reduxjs/toolkit';
const counterReducer = createReducer(0, {
increment: state => state + 1,
decrement: state => state - 1
});
Other packages similar to @reduxjs/toolkit
redux-thunk
redux-thunk is a middleware that allows you to write action creators that return a function instead of an action. It is less feature-rich compared to @reduxjs/toolkit, which includes thunk functionality out of the box.
redux-saga
redux-saga is a library that aims to make application side effects (i.e., asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, easy to test, and better at handling failures. It uses a different approach than @reduxjs/toolkit's thunks, relying on ES6 generators.
immer
immer is a package that allows you to work with immutable state in a more convenient way by writing code that mutates the state. @reduxjs/toolkit uses Immer internally to simplify reducer logic.
reselect
reselect is a library for creating memoized, composable selector functions. @reduxjs/toolkit does not include it by default but is often used alongside it for optimizing state selection.
Redux Toolkit
The official, opinionated, batteries-included toolset for efficient Redux development
(Formerly known as "Redux Starter Kit")
Installation
Using Create React App
The recommended way to start new apps with React and Redux Toolkit is by using the official Redux+JS template for Create React App, which takes advantage of React Redux's integration with React components.
npx create-react-app my-app --template redux
Or if you are a TypeScript user, use cra-template-redux-typescript, which is based on that template
npx create-react-app my-app --template redux-typescript
An Existing App
Redux Toolkit is available as a package on NPM for use with a module bundler or in a Node application:
npm install @reduxjs/toolkit
yarn add @reduxjs/toolkit
It is also available as a precompiled UMD package that defines a window.RTK
global variable.
The UMD package can be used as a <script>
tag directly.
Purpose
The Redux Toolkit package is intended to be the standard way to write Redux logic. It was originally created to help address three common concerns about Redux:
- "Configuring a Redux store is too complicated"
- "I have to add a lot of packages to get Redux to do anything useful"
- "Redux requires too much boilerplate code"
We can't solve every use case, but in the spirit of create-react-app
, we can try to provide some tools that abstract over the setup process and handle the most common use cases, as well as include some useful utilities that will let the user simplify their application code.
Because of that, this package is deliberately limited in scope. It does not address concepts like "reusable encapsulated Redux modules", folder or file structures, managing entity relationships in the store, and so on.
Redux Toolkit also includes a powerful data fetching and caching capability that we've dubbed "RTK Query". It's included in the package as a separate set of entry points. It's optional, but can eliminate the need to hand-write data fetching logic yourself.
What's Included
Redux Toolkit includes these APIs:
configureStore()
: wraps createStore
to provide simplified configuration options and good defaults. It can automatically combine your slice reducers, add whatever Redux middleware you supply, includes redux-thunk
by default, and enables use of the Redux DevTools Extension.createReducer()
: lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the immer
library to let you write simpler immutable updates with normal mutative code, like state.todos[3].completed = true
.createAction()
: generates an action creator function for the given action type string. The function itself has toString()
defined, so that it can be used in place of the type constant.createSlice()
: combines createReducer()
+ createAction()
. Accepts an object of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types.createListenerMiddleware()
: lets you define "listener" entries that contain an "effect" callback with additional logic, and a way to specify when that callback should run based on dispatched actions or state changes. A lightweight alternative to Redux async middleware like sagas and observables.createAsyncThunk()
: accepts an action type string and a function that returns a promise, and generates a thunk that dispatches pending/resolved/rejected
action types based on that promisecreateEntityAdapter()
: generates a set of reusable reducers and selectors to manage normalized data in the store- The
createSelector()
utility from the Reselect library, re-exported for ease of use.
RTK Query
RTK Query is provided as an optional addon within the @reduxjs/toolkit
package. It is purpose-built to solve the use case of data fetching and caching, supplying a compact, but powerful toolset to define an API interface layer for your app. It is intended to simplify common cases for loading data in a web application, eliminating the need to hand-write data fetching & caching logic yourself.
RTK Query is built on top of the Redux Toolkit core for its implementation, using Redux internally for its architecture. Although knowledge of Redux and RTK are not required to use RTK Query, you should explore all of the additional global store management capabilities they provide, as well as installing the Redux DevTools browser extension, which works flawlessly with RTK Query to traverse and replay a timeline of your request & cache behavior.
RTK Query is included within the installation of the core Redux Toolkit package. It is available via either of the two entry points below:
import { createApi } from '@reduxjs/toolkit/query'
import { createApi } from '@reduxjs/toolkit/query/react'
What's included
RTK Query includes these APIs:
createApi()
: The core of RTK Query's functionality. It allows you to define a set of endpoints describe how to retrieve data from a series of endpoints, including configuration of how to fetch and transform that data. In most cases, you should use this once per app, with "one API slice per base URL" as a rule of thumb.fetchBaseQuery()
: A small wrapper around fetch that aims to simplify requests. Intended as the recommended baseQuery to be used in createApi for the majority of users.<ApiProvider />
: Can be used as a Provider if you do not already have a Redux store.setupListeners()
: A utility used to enable refetchOnMount and refetchOnReconnect behaviors.
See the RTK Query Overview page for more details on what RTK Query is, what problems it solves, and how to use it.
Documentation
The Redux Toolkit docs are available at https://redux-toolkit.js.org.