import { createStore } from "redux";
//You can make store by importing createStore from redux.
const add = document.getElementById("add");
const minus = document.getElementById("minus");
const number = document.querySelector("span");
number.innerText = 0;
// It make you more safe to not make any typo when we write down in string.
const ADD = "ADD";
const MINUS = "MINUS";
const countModifier = (count = 0, action) => {
if (action.type === ADD) {
return count + 1;
} else if (action.type === MINUS) {
return count - 1;
} else {
return count;
}
};
// Same as upper code. it looks more compact and beautiful.
// switch (action.type) {
// case "ADD":
// return count + 1;
// case "MINUS":
// return count - 1;
// default:
// return count;
// }
const countStore = createStore(countModifier);
const onChange = () => {
number.innerText = countStore.getState();
};
countStore.subscribe(onChange);
//action must be type not name or anything, you can't change the name.
add.addEventListener("click", () => countStore.dispatch({ type: ADD }));
minus.addEventListener("click", () => countStore.dispatch({ type: MINUS }));
// 위의 내용과 같은 내용의 함수임.
// const handleAdd = () => {
// countStore.dispatch({type : "ADD"})
// }
// const handleMinus = () => {
// countStore.dispatch({type : "MINUS"})
// }
// add.addEventListener("click", handleAdd)
// minus.addEventListener("click", handleMinus)
조금 늦거나 다른 사람들이 푸는 문제를 풀지 못하더라도 조바심 갖지 말고 천천히 꼼꼼히 해보자!