Here is a concise summary of how to set up a Redux store with TypeScript using Redux Toolkit:
Configuring the Redux Store
1. Install the required dependencies:
npm install @reduxjs/toolkit react-redux
2. Create a `store.ts` file in your application's `src` directory. This is where you will configure the Redux store:
typescript
import { configureStore } from '@reduxjs/toolkit';
import { useDispatch } from 'react-redux';
// Import your reducers
import counterReducer from './counterSlice';
// Create the store
export const store = configureStore({
reducer: {
counter: counterReducer,
// Add more reducers here
},
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
// Create a typed version of the `useDispatch` hook
export const useAppDispatch = () => useDispatch<AppDispatch>();
3. 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;
Creating a Slice with TypeScript
1. Create a `counterSlice.ts` file in your `src` directory:
typescript
import { createSlice, PayloadAction } 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: PayloadAction<number>) => {
state.value += action.payload;
},
},
});
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export default counterSlice.reducer;
2. Import and 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;
The key points are:
1. Use `configureStore` from Redux Toolkit to create the store and define your reducers.
2. Extract the `RootState` and `AppDispatch` types from the store to use throughout your application.
3. Create typed versions of the `useDispatch` and `useSelector` hooks to simplify usage in your components.
4. Use `createSlice` from Redux Toolkit to define your reducers and actions with TypeScript types.
5. Leverage the `useAppDispatch` and `useAppSelector` hooks in your components to interact with the Redux store.
This setup provides a robust and type-safe way to use Redux with TypeScript in your React application.
Citations:[1] https://bluelight.co/blog/redux-toolkit-with-typescript
[2] https://dev.to/debosthefirst/how-to-setup-redux-toolkit-with-typescript-19g3
[3] https://redux-toolkit.js.org/tutorials/typescript
[4] https://redux.js.org/tutorials/typescript-quick-start
[5] https://www.smashingmagazine.com/2023/05/guide-redux-toolkit-typescript/