index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="src/" />
<meta charset="UTF-8" />
</head>
<body>
<div class="toggle"></div>
<hr />
<h1>0</h1>
<button id="increase">+1</button>
<button id="decrease">-1</button>
<script src="src/index.js"></script>
</body>
</html>
styles.css
.toggle {
border: 2px solid black;
width: 64px;
height: 64px;
border-radius: 32px;
box-sizing: border-box;
}
.toggle.active {
background: yellow;
}
index.js
import "./styles.css";
console.log("hello parcel");
const divToggle = document.querySelector(".toggle");
const counter = document.querySelector("h1");
const btnIncrease = document.querySelector("#increase");
const btnDecrease = document.querySelector("#decrease");
import "./styles.css";
const divToggle = document.querySelector(".toggle");
const counter = document.querySelector("h1");
const btnIncrease = document.querySelector("#increase");
const btnDecrease = document.querySelector("#decrease");
const TOGGLE_SWITCH = 'TOGGLE_SWITCH';
const INCREASE = 'INCREASE';
const DECREASE = 'DECREASE';
//액션
const toggle_switch = ()=>({type:TOGGLE_SWITCH});
const increase = difference=>({type:TOGGLE_SWITCH, difference});
const decrease = ()=>({type:DECREASE});
//액션 생성 함수
const initialState={
toggle:false,
counter:0
};
//초기값 설정
import "./styles.css";
const divToggle = document.querySelector(".toggle");
const counter = document.querySelector("h1");
const btnIncrease = document.querySelector("#increase");
const btnDecrease = document.querySelector("#decrease");
const TOGGLE_SWITCH = "TOGGLE_SWITCH";
const INCREASE = "INCREASE";
const DECREASE = "DECREASE";
//액션 생성
const toggle_switch = () => ({ type: TOGGLE_SWITCH });
const increase = (difference) => ({ type: TOGGLE_SWITCH, difference });
const decrease = () => ({ type: DECREASE });
//액션 생성 함수 생성
const initialState = {
toggle: false,
counter: 0
};
//초기값 설정
function reducer(state = initialState, action) {
switch (action.type) {
case TOGGLE_SWITCH:
return {
...state,
toggle: !state.toggle
};
case INCREASE:
return {
...state,
counter: state.counter + action.difference
};
case DECREASE:
return {
...state,
counter: state.counter - 1
};
default:
return state;
}
}
const store = createStore(reducer);
// 스토어 생성
const render = () => {
const state = store.getState();
//현재 상태 불러옴
if (state.toggle) {
divToggle.classList.add("active");
} else {
divToggle.classList.remove("active");
}
//토글 처리
counter.innerText = state.counter;
//카운터 처리
};
render();
//렌더함수 생성
store.subscribe(render);
//구독
divToggle.onclick = () => {
store.dispatch(toggle_switch());
};
btnIncrease.onclick = () => {
store.dispatch(increase(1));
};
btnDecrease.onclick = () => {
store.dispatch(decrease());
};
//액션 발생
import { createStore } from "redux";
import "./styles.css";
const divToggle = document.querySelector(".toggle");
const counter = document.querySelector("h1");
const btnIncrease = document.querySelector("#increase");
const btnDecrease = document.querySelector("#decrease");
const TOGGLE_SWITCH = "TOGGLE_SWITCH";
const INCREASE = "INCREASE";
const DECREASE = "DECREASE";
//액션 생성
const toggle_switch = () => ({ type: TOGGLE_SWITCH });
const increase = (difference) => ({ type: TOGGLE_SWITCH, difference });
const decrease = () => ({ type: DECREASE });
//액션 생성 함수 생성
const initialState = {
toggle: false,
counter: 0
};
//초기값 설정
function reducer(state = initialState, action) {
switch (action.type) {
case TOGGLE_SWITCH:
return {
...state,
toggle: !state.toggle
};
case INCREASE:
return {
...state,
counter: state.counter + action.difference
};
case DECREASE:
return {
...state,
counter: state.counter - 1
};
default:
return state;
}
}
//리듀서 함수 생성
const store = createStore(reducer);
// 스토어 생성
const render = () => {
const state = store.getState();
//현재 상태 불러옴
if (state.toggle) {
divToggle.classList.add("active");
} else {
divToggle.classList.remove("active");
}
//토글 처리
counter.innerText = state.counter;
//카운터 처리
};
render();
//렌더함수 생성
store.subscribe(render);
//구독
divToggle.onclick = () => {
store.dispatch(toggle_switch());
};
btnIncrease.onclick = () => {
store.dispatch(increase(1));
};
btnDecrease.onclick = () => {
store.dispatch(decrease());
};
//액션 발생