Redux Toolkit (RTK) is the "official, opinionated, batteries-included toolset for efficient Redux development".
RTK resolves many of the arguments against Redux related to boilerplate and unnecessary code.
redux-store.png
Redux Flow

The Redux Flow is made up of 3 main components:
Actions –– JS objects with a required type property (among others)
type is simply a string that describes the action (what happened to the state) (ie. ADD_TODO).
They aren't responsible for changing state.
An action is dispatched via the store.dispatch(action) method
// action to add a todo item
{
type: 'ADD_TODO',
text: 'This is a new todo'
}
//action that pass a login payload
{
type: 'LOGIN',
payload: {
username: 'foo',
password: 'bar'
}
}
Reducers –– pure functions that handle updating the state
performs the operations on it as instructed by the action
responsible for changing the value of the state
SWITCH-CASE for the action's type
// takes in the current state and action
// updates the value based on the action's type
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case 'INCREASE':
return { value: state.value + 1 }
case 'DECREASE':
return { value: state.value - 1 }
default:
return state
}
}
Store –– where all the states are managed.
Finally, the state will be updated in the Store
The components must be subscribed to the Store to listen for state updates to render the states correctly in the UI.
It can be created in a single line:
const store = createStore(myComponent);
Step 1: Installation + Setup
You can either install RTK:
along side create-react-app at a project's initial setup, OR
// src/store/store.js
import { configureStore } from '@reduxjs/toolkit'
export default configureStore({
reducer: {} //add reducers here
})
configureStore replaces the original createStore from Redux. It not only creates a store, but it can also accept reducer functions as arguments.
Step 3: Provide Store to React Components
We need every component to have access to the Store created. This is achieved using Provider in index.js (topmost component) (much like useContext)
// index.js
import store from './store';
import { Provider } from 'react-redux';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>
);
Step 4: Write Reducers + Actions
You can now write reducer functions (responsible for updating state based on action.type) and actions (JS objects with type-property) for our Redux Store.
Typically, you would write reducers + actions separately. But in RTK, both can be written in what's called a slice.
Slice –– collection of actions + reducers wrapped up inside the same file.