☝🏻 사용하기
- 설치
yarn add @reduxjs/toolkit
- 임포트
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
☝🏻 리덕스 사용
- 1개의 액션을 생성해도 (액션 타입 / 액션 생성 함수 / 리듀서)가 필요
- redux-actions : 많아지는 액션을 관리
- immer : 불변성 보존
- reselect : store 값을 효율적으로 핸들링해 불필요한 리렌더링을 막음
- thunk, saga : 비동기 작업
//import { createStore } from 'redux';
//import rootReducer from './module/rootReducer';
import { configureStore } from '@reduxjs/toolkit';
// before
//const devTools = window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__();
//const store = createStore(rootReducer, devTools);
// after
const store = configureStore({ reducer: rootReducer });
createSlice({
//액션의 경로를 잡아줄 이름
name: 'reducerName',
//초기 state
initialState: [],
//acition으로 구분을 준 reducer
reducers: {
//action을 선언하고 해당 action이 dispatch되면 state를 가지고 action을 처리한다.
action1(state, payload) {
//action1 logic
},
action2(state, payload) {
//action2 logic
},
action3(state, payload) {
//action3 logic
}
}
})
☝🏻 immer?
//initialState
const initialState = {
list: [],
};
//리덕스
const review = createSlice({
name: "review",
initialState,
reducers: {
addReview: (state, action) => {
const title = action.payload.title;
const content = action.payload.content;
state.list.push({ title, content });
},
getReview: (state, action) => {
state.list = action.payload;
},
deleteReview: (state, action) => {
let idx = state.list.findIndex((r) => r._id === action.payload);
if (idx !== -1) {
state.list.splice(idx, 1);
}
},
updateReview: (state, action) => {},
},
});
export const { addReview, getReview, deleteReview, updateReview } =
review.actions;
export default review;