리덕스 쓰는 이유: 기능 하나 구현하는데 prop 5개 넘겨준적이 있다. 게중 3개를 또 다른 컴포넌트에 넘겨주고. 이래서는 안된다는 생각이 들었다.
다른 선택지도 많이 있다. 실제 구현할 때는 recoil을 쓸 생각이지만 모르면 안될것 같다.
nomad coders 의 초보자를 위한 리덕스101
import { createStore } from "redux";
const ADD = "ADD";
const DELETE = "DELETE";
const addTodo = (text) => {
return {
type: ADD,
text,
};
};
const deleteTodo = (id) => {
return {
type: DELETE,
id: parseInt(id),
};
};
const reducer = (state = [], action) => {
switch (action.type) {
case ADD:
return [{ text: action.text, id: Date.now() }, ...state];
case DELETE:
return state.filter((toDo) => toDo.id !== action.id);
default:
return state;
}
};
const store = createStore(reducer);
export const actionCreators = {
addTodo,
deleteTodo,
};
export default store;