π§ Why Redux Toolkit?
If you've ever worked with plain Redux, you know it can be... a lot. Redux Toolkit (RTK) is the official, modern way to write Redux logicβboilerplate-free, scalable, and fun!
π― What We'll Build
A simple app that:
- Manages a counter
- Tracks user login state
- Uses multiple slices
- Follows best practices for scalability
This can be your starter template for any scalable app.
π¦ Step 1: Install the Dependencies
npm install @reduxjs/toolkit react-redux
Or with yarn:
yarn add @reduxjs/toolkit react-redux
π§© Step 2: Create Redux Slices
π’ counterSlice.js
// src/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; },
reset: state => { state.value = 0; }
}
});
export const { increment, decrement, reset } = counterSlice.actions;
export default counterSlice.reducer;
π userSlice.js
// src/features/user/userSlice.js
import { createSlice } from '@reduxjs/toolkit';
const userSlice = createSlice({
name: 'user',
initialState: { name: '', loggedIn: false },
reducers: {
login: (state, action) => {
state.name = action.payload;
state.loggedIn = true;
},
logout: state => {
state.name = '';
state.loggedIn = false;
}
}
});
export const { login, logout } = userSlice.actions;
export default userSlice.reducer;
ποΈ Step 3: Configure Store
// src/app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
import userReducer from '../features/user/userSlice';
const store = configureStore({
reducer: {
counter: counterReducer,
user: userReducer,
},
});
export default store;
π οΈ Step 4: Wrap App with Provider
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import store from './app/store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
π§ͺ Step 5: Build Components Using Redux Logic
// src/components/Dashboard.jsx
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement, reset } from '../features/counter/counterSlice';
import { login, logout } from '../features/user/userSlice';
function Dashboard() {
const dispatch = useDispatch();
const counter = useSelector(state => state.counter.value);
const user = useSelector(state => state.user);
return (
<div style={{ padding: 20 }}>
<h1>π₯ Redux Toolkit App</h1>
<h2>Counter: {counter}</h2>
<button onClick={() => dispatch(increment())}>+ Increment</button>
<button onClick={() => dispatch(decrement())}>- Decrement</button>
<button onClick={() => dispatch(reset())}>Reset</button>
<hr />
{user.loggedIn ? (
<>
<h2>Welcome, {user.name}!</h2>
<button onClick={() => dispatch(logout())}>Logout</button>
</>
) : (
<>
<h2>Please log in</h2>
<button onClick={() => dispatch(login('John Doe'))}>Login as John</button>
</>
)}
</div>
);
}
export default Dashboard;
π§© Add More Functionality (Optional)
β Theme Slice
// src/features/theme/themeSlice.js
import { createSlice } from '@reduxjs/toolkit';
const themeSlice = createSlice({
name: 'theme',
initialState: { darkMode: false },
reducers: {
toggleTheme: state => {
state.darkMode = !state.darkMode;
}
}
});
export const { toggleTheme } = themeSlice.actions;
export default themeSlice.reducer;
And in store.js:
import themeReducer from '../features/theme/themeSlice';
...
reducer: {
counter: counterReducer,
user: userReducer,
theme: themeReducer
}
Use it in your component just like the others!
β Folder Structure Suggestion
src/
β
βββ app/
β βββ store.js
βββ features/
β βββ counter/
β β βββ counterSlice.js
β βββ user/
β β βββ userSlice.js
β βββ theme/
β βββ themeSlice.js
βββ components/
β βββ Dashboard.jsx
βββ App.jsx
π§ Tips for Production-Ready Apps
- Use
createAsyncThunkfor async API calls - Use
RTK Queryfor advanced API integration (built-in!) - Persist Redux state with
redux-persist - Use TypeScript for type safety in large apps
π Final Thoughts
Redux Toolkit makes managing app state easy, powerful, and less verbose. The combination of slices and the configureStore abstraction provides a clean way to build scalable apps.
Top comments (0)