Redux Toolkit: The Opinionated Way to Write Redux
Redux Toolkit (RTK) is the official, opinionated way to write Redux, bundling best practices to eliminate boilerplate. Use it for any new Redux app to simplify store setup and state updates.
WHY IT EXISTS: Classic Redux required a lot of manual setup: configuring the store, adding middleware, writing separate action creators and reducers, and manually ensuring immutable updates. This led to significant boilerplate and was a common source of bugs. Redux Toolkit was created to solve these problems by providing a standardized, efficient, and less error-prone way to write Redux code.
THE MENTAL MODEL: Think of Redux Toolkit as the official "batteries-included" version of Redux. Instead of giving you the raw parts and making you assemble them from scratch, RTK gives you a pre-assembled kit with sensible defaults. It bundles the tools everyone was using anyway, like Immer for immutability and Redux Thunk for async logic, into a single, cohesive package.
HOW IT WORKS: RTK provides several key functions. configureStore simplifies store setup, automatically including default middleware and enabling the Redux DevTools Extension. The most powerful feature is createSlice, which takes a name, initial state, and reducer functions. It automatically generates action creators and action types, drastically reducing boilerplate. Inside these reducers, you can write code that looks like it's mutating state (e.g., state.value += 1), but RTK uses the Immer library to translate this into a correct, immutable update.
WHEN TO USE IT: You should use Redux Toolkit for all new Redux applications. It is the official, recommended approach for writing Redux logic. It simplifies store setup, reducer logic, and data fetching, making your code cleaner and easier to maintain in both React and React Native apps.
WHEN NOT TO USE IT: There are few reasons to avoid RTK for a new project. You might skip it only if you are working on a very old, legacy Redux codebase that is too difficult to migrate. If you've decided on Redux for your project, you should be using RTK.
ONE CANONICAL EXAMPLE: Instead of writing separate files for action types, action creators, and a reducer, RTK's createSlice combines them. A counter slice might look like this: const counterSlice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { increment: state => { state.value += 1 } } }). From this, RTK automatically generates an increment action creator and a reducer that handles the counter/increment action type, all while ensuring immutability.
Read the original → redux-toolkit.js.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.