1. Introduction & use cases
Redux keeps your application's shared state in one store. Components read data with selectors and change it only by dispatching actions, which pure reducers handle. This makes state changes predictable, traceable and easy to debug with the Redux DevTools (including time-travel).
Today Redux is written with Redux Toolkit (RTK), the official package that removes most boilerplate: configureStore, createSlice and createAsyncThunk, plus RTK Query for fetching and caching API data. Redux is a client-side library, so deployment is the same as for any React app.
Where it is used
- Large React apps where many screens share the same data (user, cart, settings)
- Complex forms, editors and dashboards with undo/history
- Caching server data with RTK Query instead of hand-written fetch code
- Apps that need detailed debugging of every state change
2. Prerequisites
| You need | Version / notes |
|---|---|
| Node.js | Current LTS release (check with node -v). Download from nodejs.org or install with nvm. |
| A React project | Created with Vite – see the ReactJS guide |
| Redux DevTools | Browser extension for Chrome/Firefox/Edge |
| Basics | React components, props, hooks (useState, useEffect) |
3. Local setup
1Option A: start from the official template
The Redux team maintains a Vite + TypeScript + Redux Toolkit template with tests already set up.
BASHnpx degit reduxjs/redux-templates/packages/vite-template-redux my-redux-app cd my-redux-app npm install npm run dev2Option B: add Redux to an existing React app
Install Redux Toolkit and the React bindings.
BASHnpm install @reduxjs/toolkit react-redux3Install Redux DevTools
Add the Redux DevTools extension to your browser.
configureStoreconnects to it automatically in development.
4. Create & run a project
1Create a slice
A slice holds the state, reducers and generated action creators for one feature. RTK lets you "mutate" state inside reducers – Immer turns it into safe immutable updates.
src/features/counter/counterSlice.jsimport { createSlice } from '@reduxjs/toolkit'; const counterSlice = createSlice({ name: 'counter', initialState: { value: 0 }, reducers: { increment: (state) => { state.value += 1; }, addBy: (state, action) => { state.value += action.payload; }, }, }); export const { increment, addBy } = counterSlice.actions; export default counterSlice.reducer;2Create the store
Combine the slices in
configureStore.src/store.jsimport { configureStore } from '@reduxjs/toolkit'; import counterReducer from './features/counter/counterSlice'; export const store = configureStore({ reducer: { counter: counterReducer }, });3Provide the store to React
Wrap the app with
<Provider>once, in the entry file.src/main.jsximport { createRoot } from 'react-dom/client'; import { Provider } from 'react-redux'; import { store } from './store'; import App from './App'; createRoot(document.getElementById('root')).render( <Provider store={store}><App /></Provider> );4Read and update state in a component
useSelectorreads data;useDispatchsends actions.src/App.jsximport { useSelector, useDispatch } from 'react-redux'; import { increment, addBy } from './features/counter/counterSlice'; export default function App() { const value = useSelector((s) => s.counter.value); const dispatch = useDispatch(); return ( <main> <h1>Count: {value}</h1> <button onClick={() => dispatch(increment())}>+1</button> <button onClick={() => dispatch(addBy(5))}>+5</button> </main> ); }5Fetch API data with RTK Query
RTK Query generates hooks with loading/error state and caching. Add
api.reducerandapi.middlewareto the store.src/services/api.jsimport { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; export const api = createApi({ baseQuery: fetchBaseQuery({ baseUrl: import.meta.env.VITE_API_URL }), endpoints: (build) => ({ getUsers: build.query({ query: () => '/users' }), }), }); export const { useGetUsersQuery } = api; // store.js // reducer: { counter: counterReducer, [api.reducerPath]: api.reducer }, // middleware: (getDefault) => getDefault().concat(api.middleware),
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Redux itself needs no configuration; your API base URL comes from Vite variables (prefix VITE_). These are compiled into the public bundle – do not store secrets.
VITE_API_URL=http://localhost:8000/apiKey dependencies
| Package / tool | Used for |
|---|---|
@reduxjs/toolkit | configureStore, createSlice, createAsyncThunk, RTK Query |
react-redux | Provider, useSelector, useDispatch |
Redux DevTools extension | Inspect actions and state, time-travel debugging |
vitest + @testing-library/react | Test reducers and connected components |
Folder structure
src/
├── app/
│ └── store.js # configureStore
├── features/
│ ├── counter/
│ │ ├── counterSlice.js
│ │ └── Counter.jsx
│ └── users/
│ └── usersSlice.js
├── services/
│ └── api.js # RTK Query
└── main.jsx # <Provider store={store}>Essential commands
| Command | What it does |
|---|---|
npm install @reduxjs/toolkit react-redux | Add Redux to a React project |
npm run dev | Develop with hot reload (state is kept across edits) |
npm test | Run reducer/selector tests (template uses Vitest) |
npm run build | Production build in dist/ |
6. Production deployment
Redux runs inside the browser bundle, so a Redux app is deployed exactly like a React SPA: build static files and serve them with Nginx, a CDN or a static host.
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Server | Any static file host (Nginx VPS, S3 + CloudFront, Netlify, Vercel) |
| Build | Node.js LTS in CI or locally |
| SPA routing | Fallback to index.html for client-side routes |
| Domain + SSL | DNS A record + Let’s Encrypt |
Deployment steps
1Build
Production builds drop the DevTools connection automatically (
configureStoreenables it only outside production unless you setdevTools).BASHnpm ci npm run build2Prepare an Ubuntu server
Create a VPS (AWS EC2, DigitalOcean, Hostinger VPS, Linode …) with Ubuntu 24.04 LTS, at least 1 vCPU and 1–2 GB RAM. Log in with SSH, update the system and create a non-root user for the app.
BASHssh root@YOUR_SERVER_IP apt update && apt upgrade -y adduser deploy usermod -aG sudo deploy # copy your SSH key to the new user, then log in as deploy rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy exit ssh deploy@YOUR_SERVER_IP3Upload `dist/`
Copy the build to the web root.
BASHrsync -avz --delete dist/ deploy@YOUR_SERVER_IP:/var/www/example.com/4Serve the build with Nginx
Copy the build output to the server and let Nginx serve it. The
try_files … /index.htmlline sends every unknown path toindex.html, so client-side routes (React Router, Vue Router) work after a page refresh./etc/nginx/sites-available/example.comserver { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri/ /index.html; } location ~* \.(js|css|png|jpg|jpeg|gif|svg|webp|woff2?)$ { expires 30d; add_header Cache-Control "public, immutable"; } }5Enable the site and reload Nginx
Link the config into
sites-enabled, test it and reload.nginx -tmust say syntax is ok before you reload.BASHsudo apt install -y nginx sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx6Point your domain to the server
In your domain registrar's DNS panel add an A record for
@(andwww) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check withdigor an online DNS checker.OUTPUT# Type Name Value TTL # A @ 203.0.113.10 3600 # A www 203.0.113.10 3600 dig +short example.com7Add free HTTPS with Let’s Encrypt
Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with
--dry-run.BASHsudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d example.com -d www.example.com sudo certbot renew --dry-run
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
could not find react-redux context value; please ensure the component is wrapped in a <Provider>
Wrap the root component with <Provider store={store}> in main.jsx, and make sure tests render components inside a Provider too.
A non-serializable value was detected in the state
Keep only plain data (objects, arrays, strings, numbers) in Redux – not Promises, class instances, DOM nodes or Date objects (store ISO strings instead).
Component does not re-render after dispatch
You returned a new value and mutated, or mutated outside a reducer. Inside createSlice either mutate the draft or return a new object – not both.
useSelector causes re-renders on every update
Return primitive values or memoised selectors (createSelector) instead of building new objects/arrays inside the selector.
RTK Query hook always shows old data after a mutation
Use providesTags on queries and invalidatesTags on mutations so the cache refreshes.
Best practices
- Put only truly shared state in Redux; keep form input and UI toggles in component state.
- One slice per feature, colocated with its components ("feature folders").
- Use RTK Query for server data instead of writing fetch thunks by hand.
- Write unit tests for reducers – they are pure functions and easy to test.
- Use TypeScript types
RootStateandAppDispatchfor typed hooks in larger apps.