yarn add @reduxjs/toolkit react-redux
🔻 수정 전
import { createStore } from "redux";
import { combineReducers } from "redux";
import todos from "../modules/todos";
// 1. create rootReducer with reducers
const rootReducer = combineReducers({
todos,
});
// 2. create store
const store = createStore(rootReducer);
});
// 3. export
export default store;
🔻수정 후
import todos from "../modules/todos";
import { configureStore } from "@reduxjs/toolkit";
const store = configureStore({
reducer: {
todos,
}
});
export default store;
import { createSlice } from "@reduxjs/toolkit";
import { v4 as uuidv4 } from "uuid";
// initial states
const initialState = [
{
id: uuidv4(),
title: "리액트 공부하기",
contents: "빨리빨리 암기하기",
isDone: false,
},
{
id: uuidv4(),
title: "스프링 공부하기",
contents: "인강 열심히 들어보기!!",
isDone: true,
},
{
id: uuidv4(),
title: "데이트",
contents: "홍대입구역에서 3시까지",
isDone: false,
},
];
const todosSlice = createSlice({
name: "todos",
initialState,
reducers: {
addTodo: (state, action) => {
state.push(action.payload);
},
removeTodo: (state, action) => {
return state.filter((item) => item.id !== action.payload);
},
switchTodo: (state, action) => {
return state.map((item) => {
if (item.id === action.payload) {
return { ...item, isDone: !item.isDone };
} else {
return item;
}
});
},
},
});
export const {addTodo, removeTodo, switchTodo} = todosSlice.actions;
export default todosSlice.reducer;
[...state, action.payload]
state.push(action.payload)
사용 가능