Redux Toolkit Essentials
0 525
๐งฐ Redux Toolkit Essentials
Redux Toolkit (RTK) is the official, recommended way to write Redux logic in modern React apps. If Redux felt boilerplate-heavy before โ good news! RTK simplifies state management, improves developer experience, and works seamlessly with React 19. โ๏ธ๐
๐ Why Redux Toolkit?
- ๐ง Built-in support for best practices
- โ๏ธ Reduces boilerplate (no more verbose action files)
- ๐งช Great for debugging and DevTools integration
- ๐ฆ Comes with utilities like
createSlice,configureStore,createAsyncThunk
๐ฆ Step 1: Installation
npm install @reduxjs/toolkit react-redux
๐ Step 2: Create a Slice
A slice contains your reducer logic and actions โ all in one file!
// features/counter/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1 },
decrement: (state) => { state.value -= 1 },
incrementByAmount: (state, action) => {
state.value += action.payload;
}
}
});
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export default counterSlice.reducer;
โ No need to manually create action types โ RTK does it for you!
๐ ๏ธ Step 3: Configure the Store
// app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
const store = configureStore({
reducer: {
counter: counterReducer
}
});
export default store;
๐ Step 4: Connect to React
// index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { Provider } from 'react-redux';
import store from './app/store';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<App />
</Provider>
);
โ๏ธ This provides Redux state to your entire React app.
๐งโ๐ป Step 5: Using State and Actions in Components
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './features/counter/counterSlice';
function Counter() {
const count = useSelector((state) => state.counter.value);
const dispatch = useDispatch();
return (
<div>
<h1>{count}</h1>
<button onClick={() => dispatch(increment())}>+</button>
<button onClick={() => dispatch(decrement())}>-</button>
</div>
);
}
๐ก Async with createAsyncThunk
Redux Toolkit includes createAsyncThunk for handling API calls.
import { createAsyncThunk } from '@reduxjs/toolkit';
export const fetchUser = createAsyncThunk(
'user/fetchUser',
async (userId) => {
const response = await fetch(`/api/user/${userId}`);
return await response.json();
}
);
๐ง Handling Async States in Slice
const userSlice = createSlice({
name: 'user',
initialState: { data: null, status: 'idle' },
extraReducers: (builder) => {
builder
.addCase(fetchUser.pending, (state) => {
state.status = 'loading';
})
.addCase(fetchUser.fulfilled, (state, action) => {
state.status = 'succeeded';
state.data = action.payload;
})
.addCase(fetchUser.rejected, (state) => {
state.status = 'failed';
});
}
});
๐งฉ RTK manages async loading states and error handling cleanly โ no need for external middleware like redux-thunk or redux-saga.
๐ Summary: Redux Toolkit Highlights
- ๐ฆ
createSlicesimplifies action & reducer creation - ๐งฉ
configureStoreadds DevTools + middleware by default - ๐
createAsyncThunkhandles async actions effortlessly - โ๏ธ Fully compatible with React 19 and server/client components
๐ Final Thoughts
Redux Toolkit makes Redux simple, clean, and powerful. Whether you're building a large-scale app or just want predictable global state, RTK is the go-to solution. It embraces modern React patterns, handles async with ease, and eliminates the clutter. ๐ง ๐
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments