우리는 단순히 한개의 컴포넌트에서만 이루어지는 Local State, 또는 Property로 data를 주고받는 Cross-Compoents State, 또는 App 전체에 영향을 주는 유형검사 또는 context를 사용했다.
const redux = require("redux");
const counterReducer = (state={counter:0}, action) => {
if(action.type==="increment"){
return {
counter: state.counter + 1,
};
};
if(action.type==="decrement"){
return {
counter: state.counter - 1,
};
};
return state;
};
const store = redux.createStore(counterReducer);
const counterSubscriber = () => {
const latestState = store.getState();
console.log(latestState);
};
store.subscribe(counterSubscriber);
store.dispatch({type:"increment"});
store.dispatch({type:"decrement"});
### result
{ counter : 1 }
{ counter : 0 }
import redux from "redux"; // => redux 전체 가져오기
import {createStore} from "redux"; // => redux library의 특정 부분 가져오기
const initialState = { counter: 0, showCounter: true };
const counterReducer = (state = initialState, action) => {
if (action.type === "increment") {
return { counter: state.counter + 1, showCounter: state.showCounter };
}
if (action.type === "increase") {
return {
counter: state.counter + action.value,
showCounter: state.showCounter,
};
}
if (action.type === "decrement") {
return { counter: state.counter - 1, showCounter: state.showCounter };
}
if (action.type === "toggle") {
return { counter: state.counter, showCounter: !state.showCounter };
}
return state;
};
const store = createStore(counterReducer);
export default store;
<App/>
을 감싸주어야한다.import React from "react";
import ReactDOM from "react-dom/client";
import { Provider } from "react-redux";
import "./index.css";
import App from "./App";
import store from "./store/reduxLogix";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
<App />
</Provider>
);
useSelector(state=>state.counter);
import classes from "./Counter.module.css";
import { useSelector, useDispatch } from "react-redux";
const Counter = () => {
const dispatch = useDispatch();
const counter = useSelector((state) => state.counter);
const show = useSelector((state) => state.showCounter);
const incrementHandler = () => {
dispatch({ type: "increment" });
};
const increaseHandler = () => {
dispatch({ type: "increase", value: 5 });
};
const decrementHandler = () => {
dispatch({ type: "decrement" });
};
const toggleCounterHandler = () => {
dispatch({ type: "toggle" });
};
return (
<main className={classes.counter}>
<h1>Redux Counter</h1>
{show && <div className={classes.value}>{counter}</div>}
<button onClick={increaseHandler}>더하기 +5</button>
<button onClick={incrementHandler}>더하기 +1</button>
<button onClick={decrementHandler}>빼기 -1</button>
<button onClick={toggleCounterHandler}>Toggle Counter</button>
</main>
);
};
export default Counter;
const counterReducer = (state = initialState, action) => {
if (action.type === "increment") {
return { counter: state.counter + 1, showCounter: state.showCounter };
}
return state;
};
const counterReducer = (state = initialState, action) => {
if (action.type === "increment") {
state.counter++;
return state;
(또는)return { counter: state.counter, showCounter: state.showCounter };
}
return state;
};
즉, 정말 중요한 것은 절대 기존의 state 형식을 변경해선 안 된다. 따라서 항상 새로운 객체가 배열을 만들어야한다.
import {Component} from "react";
import {connect} from "react-redux";
class Counter extends Components{
incrementHandler(){
this.props.increment();
}
decrementHandler(){
this.props.decrement();
}
toggleCounterHandler(){}
render(){
return (
<main className={classes.counter}>
<h1>Redux Counter</h1>
<div className={classes.value}>{this.props.counter}</div>
<button onClick={this.incrementHandler.bind(this)}>더하기 +1</button>
<button onClick={this.decrementHandler.bind(this)}>빼기 -1</button>
<button onClick={this.toggleCounterHandler}>Toggle Counter</button>
</main>
);
};
}
const mapStateToProps = state => {
return {
counter:state.counter
};
}
const mapDispatchToProps = dispatch => {
return {
increment:()=>dispatch({type:"increment"}),
decrement:()=>dispatch({type:"decrement"})
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Counter);