
To our previous little app with fake authorization, we're gonna add a feature to show users data to logged user.įirstly let's create file src/store/users.js and create our slice: import = slice. If you don't want to start from zero, you can use our basic Redux configuration with Redux Toolkit. This is because createSlice() uses the Immer library.

Redux Toolkit builds in our suggested best practices, simplifies most. The reduxjs/toolkit package wraps around the core redux package, and contains API methods and common dependencies that we think are essential for building a Redux app. But what is the most important benefit of using Toolkit? Definitely, it's createSlice function that you will probably use for most application you're developing. createSlice() lets you write immutable updates using mutation-like logic within the case reducers. Redux Toolkit (also known as 'RTK' for short) is our official recommended approach for writing Redux logic. We have made basic Redux configuration with Redux Toolkit. We can put all of our Redux-related logic for the slice into a single file. The other benefit of using createSlice is our files structure. That doesn't work well with createSlice() right now, because it tries to generate action creators and action types based on the reducer names, and the reducers only respond to those actions. reducers: it's an object where the keys will become action type strings, and the functions are reducers that will be run when that action type is dispatched. As a more specific example, let's say you want to handle async request status (ie, 'REQUESTSTARTED / SUCCESS / FAILURE' actions.initialState: the initial values for our reducer.


It's a function that deals with everything you need for each slice, do you remember using createAction and createReducer manually? Now it's available in this specific slice function.
