Redux - woowa-techcamp-2021/store-4 GitHub Wiki

https://medium.com/@jsh901220/react์—-redux-์ ์šฉํ•˜๊ธฐ-a8e6efd745c9

https://beomy.tistory.com/35

๋ฆฌ๋•์Šค ์ •์˜

โ†’ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ state container.

  • action ์— ๋ฐ˜์‘ํ•ด ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.

    โ‡’ UI ๋ฅผ ์ƒํƒœ์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋กœ ๊ธฐ์ˆ ํ•˜๋Š” react ์™€ ๊ถํ•ฉ์ด ์ข‹๋‹ค.

local state vs global state

  • local state : ๊ฐ component ๊ฐ€ ๊ฐ€์ง€๋Š” state.
  • global state : ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ๊ณต์œ ํ•˜๋Š” state. (์˜ˆ : ๋กœ๊ทธ์ธ ์ •๋ณด)

๋ฆฌ์•กํŠธ์—์„œ ์ƒํƒœ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 

  1. local state ์ „ํŒŒ๊ฐ€ ์–ด๋ ต๋‹ค.
  2. global state ์œ ์ง€๊ฐ€ ์–ด๋ ต๋‹ค.

โ‡’ ์ •๋ฆฌํ•˜๋ฉด, props ์™ธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „ํŒŒํ•  ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค

react-redux

  • dispatch ๋ฅผ โ‡’ props ๋กœ ๋งคํ•‘.
  • store ์˜ state ๋ฅผ โ‡’ props ๋กœ ๋งคํ•‘.
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { setDiff } from '../actions';

class Option extends Component {
	constructor(props) {
		super(props);
		this.onChange = this.onChange.bind(this);
	}
	
	onChange(event) {
		this.props.onUpdateDiff(parseInt(event.target.value));
	}
	
	render() {
		return (
			<div>
				<input value={this.props.diff} onChange={this.onChange} />
			</div>);
	}
}

let mapStateToProps = (state) => {
	return {
		diff: state.counter.diff
	}
}

let mapDispatchToProps = (dispatch) => {
	return {
		onUpdateDiff: (value) => dispatch(setDiff(value))
	};
}

Option = connect(mapStateToProps, mapDispatchToProps)(Option);
export default Option;
import { createStore } from "redux";
import { Provider } from "react-redux";
const store = createStore(counterApp);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);

react + redux

1. Action

  • ์–ด๋–ค ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚˜์•ผ ํ• ์ง€ ์•Œ๋ ค์ฃผ๋Š” (๊ฐ์ฒด?).
  • type ์ด ํ•„์ˆ˜์ ์œผ๋กœ ํฌํ•จ. โ‡’ action ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ID.
export const INCREMENT = 'INCREMENT'; 
export const DECREMENT = 'DECREMENT'; 
export const SET_DIFF = 'SET_DIFF'; 

export function increment() { 
	return { 
		type: INCREMENT 
	}; 
} 

export function decrement() { 
	return { 
		type: DECREMENT 
	}; 
} 

export function setDiff(value) { 
	return {
		type: SET_DIFF, 
		diff: value
	};
}

2. Reducer

  • action ์„ ๊ฑด๋„ค๋ฐ›์•„ state ๋ฅผ ๋ณ€๊ฒฝ.
import { INCREMENT, DECREMENT, SET_DIFF } from "../actions";
import { combineReducers } from "redux";

const counterInitialState = { value: 0, diff: 1 };

const counter = (state = counterInitialState, action) => {
  switch (action.type) {
    case INCREMENT:
      return Object.assign({}, state, { value: state.value + state.diff });
    case DECREMENT:
      return Object.assign({}, state, { value: state.value - state.diff });
    case SET_DIFF:
      return Object.assign({}, state, { diff: action.diff });
    default:
      return state;
  }
};

const counterApp = combineReducers({ counter });
export default counterApp;

dispatch

  • ์–ด๋–ค ์ด๋ฒคํŠธ ๋ฐœ์ƒ โ†’ store.dispatch(action) โ†’ reducer ๊ฐ€ state ๋ณ€๊ฒฝ.
onChange(event) { 
	this.props.store.dispatch(setDiff(parseInt(event.target.value))); 
}

onIncrement(event) {
	this.props.store.dispatch(increment())
}

onDecrement(event) {
	this.props.store.dispatch(decrement())
}

subscribe

const store = createStore(counterApp);

const render = () => {
  ReactDOM.render(<App store={store} />, document.getElementById("root"));
};

store.subscribe(render);
render();
render() {
	return (
		<div>
			<input value={this.props.store.getState().counter.diff} onChange={this.onChange} />
		</div>
	);
}

๊ท€์ฐฎ์€ ์ .

class App extends Component {
	render() {
		return (
			<div>
				<Counter store={this.props.store} />
				<Option store={this.props.store} />
				<Button store={this.props.store} /> </div>
		);
	}
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ