Redux Toolkit
-
This lesson introduces Redux Toolkit and explains how it simplifies state management in React applications.
Redux Toolkit
Why Redux Toolkit?
Redux Toolkit (RTK) simplifies Redux.
Problems with old Redux:
Too much boilerplate
Complex setup
RTK Benefits:
✅ Less code
✅ Better performance
✅ Built-in best practicesCreating Slices
A slice contains:
State
Reducers
Actions
Example:
const counterSlice = createSlice({
name: "counter",
initialState: { value: 0 },
reducers: {
increment: state => {
state.value += 1;
}
}
});
Using Redux Toolkit with React
Steps:
Create store
Provide store using <Provider>
const count = useSelector(state => state.counter.value);
const dispatch = useDispatch();