๐Ÿ–ฅFrontEnd/Redux

NEVER MUTATE REDUX!!

hellohailie 2022. 8. 13. 10:37

โญ๏ธ๋ฆฌ๋•์Šค๋Š” ๊ผญ action์„ ํ†ตํ•ด์„œ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค!! ๋งค์šฐ ์ค‘์š”!!โญ๏ธ

 

(์•„๋ž˜ ์˜ˆ์‹œ๋Š” vanilla JS์—์„œ ์“ฐ๋Š” ๋ฆฌ๋•์Šค ์˜ˆ์‹œ ์ž…๋‹ˆ๋‹ค. )

โŒ๊ทธ๋ž˜์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ”๋กœ ์ˆ˜์ •์„ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. โŒ

const reducer = (state = [], action) => {
  switch (action.type) {
    case ADD_TODO:
      return state.push(action.text)
    case DELETE_TODO:
      return;
    default:
      return state;
  }
};

 

๐ŸŸข์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค. ๐ŸŸข

const reducer = (state = [], action) => {
  switch (action.type) {
    case ADD_TODO:
      return [...state, { text: action.text }];
    case DELETE_TODO:
      return;
    default:
      return state;
  }
};

 


์ „์ฒด ์ฝ”๋“œ

import { createStore } from "redux";

const form = document.querySelector("form");
const input = document.querySelector("input");
const ul = document.querySelector("ul");

const ADD_TODO = "ADD_TODO";
const DELETE_TODO = "DELETE_TODO";

const addTodo = (text) => {
  return { type: ADD_TODO, text };
};

const deleteTodo = (id) => {
  return { type: DELETE_TODO, id };
};

const reducer = (state = [], action) => {
  switch (action.type) {
    case ADD_TODO:
      const newTodoObj = { text: action.text, id: Date.now() };
      return [newTodoObj, ...state];
    case DELETE_TODO:
      return state.filter((todo) => todo.id !== parseInt(action.id)); // string ํ˜•ํƒœ๋ผ์„œ ๋„˜๋ฒ„๋กœ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค!!
    default:
      return state;
  }
};

const store = createStore(reducer);

store.subscribe(() => {
  console.log(store.getState());
});

const dispatchAddTodo = (text) => {
  store.dispatch(addTodo(text));
};

const dispatchDeleteTodo = (e) => {
  const id = e.target.parentNode.id;
  store.dispatch(deleteTodo(id));
};

const paintTodo = () => {
  const todo = store.getState();
  ul.textContent = ""; // ์ด๊ฑฐ ์—†์œผ๋ฉด store ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ๋ชจ๋“  ๊ฒƒ์„ repainting ํ•œ๋‹ค.
  //innerHTML ์™€ textContent ๋ฉ”์„œ๋“œ ๋ชจ๋‘ ํ…์ŠคํŠธ ๊ฐ’์„ ์ฝ์–ด์˜ค๊ณ , ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค. innerHTML๋Š” ๋ณด์•ˆ์— ์ทจ์•ฝํ•จ

  todo.forEach((todo) => {
    const li = document.createElement("li");
    const btn = document.createElement("button");
    btn.innerText = "DELETE";
    btn.addEventListener("click", dispatchDeleteTodo);
    li.id = todo.id;
    li.innerText = todo.text;
    li.appendChild(btn);
    ul.appendChild(li);
  });
};

store.subscribe(paintTodo); // todo์˜ ๋ณ€ํ™”์— ๋งž๊ฒŒ repainting ํ•˜๊ณ  ์žˆ๋‹ค.

// const createTodo = (todo) => {
//   const li = document.createElement("li");
//   li.innerText = todo;
//   ul.appendChild(li);
// };

const onSubmit = (e) => {
  e.preventDefault();
  const todo = input.value;
  input.value = "";
  //createTodo(todo);
  dispatchAddTodo(todo);
};

form.addEventListener("submit", onSubmit);

 

 

๐Ÿ˜ƒ ์ž˜๋ชป๋œ ๊ฐœ๋… ์ „๋‹ฌ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์ €์˜ ์„ฑ์žฅ์— ํฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค๐Ÿค“