store
Action
Reducer
combineReducers
usedispatch()
useSelector()
Redux는 다음과 같은 경우에 유용하다.
import { Provider } from "react-redux";
import store from "./redux/store";
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);
import { useDispatch, useSelector } from "react-redux";
import type { TypedUseSelectorHook } from "react-redux";
import type { RootState, AppDispatch } from "./store";
export const useAppDispatch = (): any => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
import { configureStore } from "@reduxjs/toolkit";
import taskBanUserSlice from "./slices/taskBanUserSlice";
const store = configureStore({
reducer: {
// reducerName: reducer
taskBanUser: taskBanUserSlice,
},
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export default store;
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { TaskBanUserData } from "../../type";
// Define the initial state using that type
const initialState: TaskBanUserData = [];
export const taskBanUserSlice = createSlice({
name: "taskBanUser",
// `createSlice` will infer the state type from the `initialState` argument
initialState,
reducers: {
// Use the PayloadAction type to declare the contents of `action.payload`
GetBanUserResponse: (state, action: PayloadAction<TaskBanUserData>) => {
return action.payload;
},
taskBanUserInitialize: (state) => {
return initialState;
},
},
});
export const { GetBanUserResponse, taskBanUserInitialize } =
taskBanUserSlice.actions;
export default taskBanUserSlice.reducer;