import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import { createWrapper } from 'next-redux-wrapper';
import authReducer from './reducers/authReducer';
import productReducer from './reducers/productReducer';
export const createStore = () => {
const store = configureStore({
reducer: { auth: authReducer, product: productReducer },
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}),
});
return store;
};
export type AppStore = ReturnType<typeof createStore>;
export type RootState = ReturnType<typeof createStore>;
export type AppDispatch = AppStore['dispatch'];
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
RootState,
unknown,
Action
>;
export const wrapper = createWrapper(createStore);
import { AppDispatch } from '../redux/store';
const dispath = useDispatch<AppDispatch>();
const productSlice = createSlice({
name: 'product',
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(getAllProducts.pending, (state, action: AnyAction) => {
console.log('pending 상태', action);
state.isLoading = true;
}),
builder.addCase(getAllProducts.fulfilled, (state, action: AnyAction) => {
console.log('fulfilled 상태', action);
state.isLoading = false;
state.productList = action.payload;
}),
builder.addCase(getAllProducts.rejected, (state, action: AnyAction) => {
console.log('rejected 상태', action);
state.isLoading = false;
state.error = action.payload;
});
},
});
dispatch
를 사용하기 위해 store.ts
에 type
설정
useDispatch
와 action
에 AppDispatch
지정
참고 - https://curryyou.tistory.com/501