To set up a Redux store with TypeScript using Redux Toolkit, follow these steps:
1. Install Dependencies:
- Install Redux Toolkit and React Redux:
bash
npm install @reduxjs/toolkit react-redux
2. Create the Redux Store:
- Create a file named `store.ts` in your application's `src` directory:
typescript
import { configureStore } from '@reduxjs/toolkit';
import { rootReducer } from './rootReducer';
export const store = configureStore({
reducer: rootReducer,
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
3. Define the Root Reducer:
- Create a file named `rootReducer.ts` in your application's `src` directory:
typescript
import { configureStore } from '@reduxjs/toolkit';
import { counterReducer } from './counterSlice';
export const rootReducer = combineReducers({
counter: counterReducer,
});
export default rootReducer;
4. Create a Slice:
- Create a file named `counterSlice.ts` in your application's `src` directory:
typescript
import { createSlice } from '@reduxjs/toolkit';
interface CounterState {
value: number;
}
const initialState: CounterState = {
value: 0,
};
const counterSlice = createSlice({
name: 'counter',
initialState,
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;
5. Wrap the App with the Redux Store:
- In your main application file (e.g., `App.tsx`), wrap your app with the `Provider` component from `react-redux` and pass the `store` instance:
typescript
import React from 'react';
import { Provider } from 'react-redux';
import { store } from './store';
function App() {
return (
<Provider store={store}>
{/* Your app components */}
</Provider>
);
}
export default App;
6. Use the Redux Store in Components:
- Use the `useAppDispatch` and `useAppSelector` hooks in your components to interact with the Redux store:
typescript
import React from 'react';
import { useAppDispatch, useAppSelector } from './store';
import { increment, decrement, incrementByAmount } from './counterSlice';
function Counter() {
const count = useAppSelector((state) => state.counter.value);
const dispatch = useAppDispatch();
return (
<div>
<button onClick={() => dispatch(decrement())}>-</button>
<span>{count}</span>
<button onClick={() => dispatch(increment())}>+</button>
<button onClick={() => dispatch(incrementByAmount(5))}>Increment by 5</button>
</div>
);
}
export default Counter;
Additional Tips
- Middleware: You can add middleware to the store by using the `configureStore` function. Middleware can be used to handle actions before they reach the reducers[2].
- Action Creators: Use the `createAction` function from Redux Toolkit to create action creators that are strongly typed[2].
- Reducers: Use the `combineReducers` function from Redux Toolkit to combine multiple reducers into one. Each reducer should be defined in a separate file and exported[2].
By following these steps and tips, you can set up a robust and type-safe Redux store with TypeScript using Redux Toolkit.
Citations:[1] https://stackoverflow.com/questions/45339448/how-do-you-create-strongly-typed-redux-middleware-in-typescript-from-reduxs-typ
[2] https://redux.js.org/usage/usage-with-typescript
[3] https://borstch.com/snippet/using-typescript-with-redux-middlewares
[4] https://dev.to/alaa-m1/react-redux-with-typescript-practical-example-12me
[5] https://bluelight.co/blog/redux-toolkit-with-typescript