์ด๋ฒ ์ข
ํฉ ํด์ฆ ์ค๋ณต ์ ๋ต์ด ๋ง์๋๋ฐ ๋ ๋ค ์๊น๊ฒ ํ๋ ์ฉ ๋น ์ ธ์
์ผ๊ฐ์์จํ์ต๋ฐ(๋ฐ๋ง๋ถ์ด๋ฐ์์ ๋ณ๊ฒฝ ๋จ)์ ์ ์ ๋์๋ค.
Redux๋ Action - Dispatch - Reducer - Store์์ผ๋ก ์งํ๋๋ค.
Provider
, createStore
๋ฅผ ๋ถ๋ฌ์์ผํ๋ค.import React from 'react';
import { createRoot } from 'react-dom/client';
// Provider ๋ถ๋ฌ์ค๊ธฐ
import { Provider } from 'react-redux';
// createStore ๋ถ๋ฌ์ค๊ธฐ
import { legacy_createStore as createStore } from 'redux';
import App from './App';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
// Action type ๋ณ์๋ก ์ ์ธํ๊ธฐ
// ์คํ ๋ฐฉ์ง, ์ฌ์ฌ์ฉ์ฑ, ์๋ ์
๋ ฅ์ ์ข๋ค.
const PLUS = 'PLUS';
const MINUS = 'MINUS';
// Action ์์ฑ์ ํจ์๋ก ๋ง๋ค์ด ๊ด๋ฆฌํ๊ธฐ
// ์ธ๋ถ์์ ์ฌ์ฉ ๊ฐ๋ฅ ํ๋๋ก export ๋ถ์ฌ์ฃผ๊ธฐ
export const increase = () => {
return {
type: PLUS,
};
};
export const decrease = () => {
return {
type: MINUS,
};
};
const count = 1;
// reducer ํจ์ ์์ฑ Action type์ ๋ฐ๋ฅธ ๋ค๋ฅธ ์ด๋ฒคํธ ์ฃผ๊ธฐ
// ์ฒซ๋ฒ์งธ ์ธ์: state, ๋๋ฒ์งธ ์ธ์: action
const reducer = (state = count, action) => {
switch (action.type) {
case PLUS:
return state + 1;
case MINUS:
return state - 1;
default:
return state;
}
};
// store ์์ฑ ํ
// reducer ํจ์ ์ธ์๋ก ์ ๋ฌ
const store = createStore(reducer);
root.render(
// Provider ์ปดํฌ๋ํธ ์์ฑ ํ
// store props๋ก ์ ๋ฌํ๊ธฐ
<Provider store={store}>
<App />
</Provider>
);
useSelector
๋ฅผ ์ด์ฉํด state
์ ์ ๊ทผํ ์ ์๋ค.useDispatch
๋ฅผ ์ด์ฉํด dispatch
ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.import React from 'react';
import './style.css';
// useSelector, useDispatch ๋ถ๋ฌ์ค๊ธฐ
import { useSelector, useDispatch } from 'react-redux';
// index.js์ ์๋ increase, decrease ๋ถ๋ฌ์ค๊ธฐ
import { increase, decrease } from './index.js';
export default function App() {
// useDispatch๋ฅผ ์ด์ฉํด dispatch ํจ์ ์ฌ์ฉ
const dispatch = useDispatch();
// useSelector๋ฅผ ์ด์ฉํด state ์ ๊ทผ
const state = useSelector((state) => state);
const plusNum = () => {
// dispatchํจ์ ์ธ์๋ Action type์ ๊ฐ
dispatch(increase());
};
const minusNum = () => {
// dispatchํจ์ ์ธ์๋ Action type์ ๊ฐ
dispatch(decrease());
};
return (
// state ๊ฐ์ผ๋ก ๋ณ๊ฒฝ ํด ์ฃผ๊ธฐ ${state}
<div className="container">
<h1>{`Count: ${state}`}</h1>
<div>
<button className="plusBtn" onClick={plusNum}>
+
</button>
<button className="minusBtn" onClick={minusNum}>
-
</button>
</div>
</div>
);
}
์ ์๋ํ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
export
๋ถ์ฌ ์ฃผ๋๊ฑธ ์๊ฒ ๋์๋คexport const increase = () => {
return {
type: PLUS,
};
};
const PLUS = 'PLUS';
const MINUS = 'MINUS';
export const increase = () => {
return {
type: PLUS,
};
};
export const decrease = () => {
return {
type: MINUS,
};
};
spread syntax(...state)
, Object.assign
๋ฑ์ ์ด์ฉํด ์์
ํด ์ค๋ค.(๊ฐ ๋ณต์ฌ)type
์ธ์ ๊ตฌ์ฒด์ ์ผ๋ก ๊ฐ์ ์ค์ ํด ์ฃผ๋ ๊ฒ์ด๋ค.payload
๋ผ๊ณ ์ด๋ฆ์ ์ง์ด์ฃผ์ง ์์๋ ๋๋ ๊ฑฐ ๊ฐ๋ค.action.type
, action.payload
๋ฑ์ผ๋ก ์์ฑํ๋ฉด ๋๋ค.export const increase = () => {
return {
type: PLUS,
payload: {
id
}
};
};
useDispatch
, useSelector
๋ฅผ ์ปดํฌ๋ํธ ์ต์์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋๋ฐ ๊ทธ ๋ฐ์ ๋ฃ์ด์ค ํ์ฐธ ํค๋งธ๋ ๊ฑฐ ๊ฐ๋ค.// ์ด๋ฐ์ ์์ฑํ ์์น
// const dispatch = useDispatch();
// const state = useSelector((state) => state);
export default function App() {
const dispatch = useDispatch();
const state = useSelector((state) => state);
return (
);
}
Redux๋ ํ๋ฆ์ ์์๋ง ์ดํดํ๋ฉด ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์๊ฐ ๋ณด๋ค ๊ฐ๋จํ๋ ๊ฒ ๊ฐ๋ค.
๋จ, ๊ทธ ํ๋ฆ์ ์ดํดํ๋๋ฐ ์ค๋ ์๊ฐ์ด ๊ฑธ๋ ธ๋ค.