Redux Toolkit Essentials
0 695
🧰 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 includescreateAsyncThunk 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