๐ [App.js]
import React from 'react';
import './style.css';
{/*
import { useDispatch, useSelector } from 'react-redux';๋ฅผ
ํตํด react-redux์์ useSelector๋ฅผ ๋ถ๋ฌ์์ฃผ์ธ์.
*/}
import {useSelector} from 'react-redux';
{/*
import { useDispatch } from 'react-redux';๋ฅผ ํตํด
react-redux์์ useDispatch๋ฅผ ๋ถ๋ฌ์์ฃผ์ธ์.
*/}
import {useDispatch} from 'react-redux';
// ๋ ๊ฐ๋ฅผ ๋์์ ๋ถ๋ฌ์ฌ ์ ์๋ค.
// import {useSelector, useDispatch} from 'react-redux';
{/*
import { increase,decrease } from './index.js';๋ฅผ ํตํด
Action Creater ํจ์ increase, decrease๋ฅผ ๋ถ๋ฌ์์ฃผ์ธ์. */}
import {increase, decrease} from './index.js';
export default function App() {
{/*
useDispatch์ ์คํ ๊ฐ์ ๋ณ์์ ์ ์ฅํด์
dispatch ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.(์ฝ์๊ฒฐ๊ณผ๋ฅผ ํ์ธํด๋ณด์ธ์!)
*/}
const dispatch = useDispatch()
console.log(dispatch);
{/*
useSeletor๋ฅผ ํตํด state๊ฐ ํ์ํ ์ปดํฌ๋ํธ์์
์ ์ญ ๋ณ์ ์ ์ฅ์ store์ ์ ์ฅ๋ state๋ฅผ ์ฝ๊ฒ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.
*/}
{/*
useSelector์ ์ฝ๋ฐฑ ํจ์์ ์ธ์์ Store์ ์ ์ฅ๋
๋ชจ๋ state๊ฐ ๋ด๊น๋๋ค.
๊ทธ๋๋ก return์ ํ๊ฒ ๋๋ฉด Store์ ์ ์ฅ๋
๋ชจ๋ state๋ฅผ ๊บผ๋ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
*/}
const state = useSelector((state) => state);
{/*
๋ณ์ state๋ฅผ ์ฝ์์์ ํ์ธํด๋ณด์ธ์.
Store์ ์ ์ฅ๋ ๊ธฐ์กด state ๊ฐ์ธ 1์ด ์ฐํ๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
*/}
console.log(state);
{/*
dispatch ํจ์๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์์ ์ฌ์ฉ๋ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ dispatch ํจ์๋ action ๊ฐ์ฒด๋ฅผ Reducer ํจ์๋ก ์ ๋ฌํด์ค๋๋ค.
*/}
{/*
์ด๋ฒคํธ ํธ๋ค๋ฌ onClick={plusNum}๊ณผ,
onClick={minusNum}์์์ dispatch๋ฅผ ํตํด
action ๊ฐ์ฒด๋ฅผ Reducer ํจ์๋ก ์ ๋ฌํด์ฃผ์ธ์.
*/}
const plusNum = () => {
dispatch(increase());
console.log(plusNum);
};
const minusNum = () => {
dispatch(decrease());
console.log(minusNum);
};
return (
<div className="container">
{/*
Store์์ ๊บผ๋ด์จ state๋ฅผ ํ๋ฉด์ ๋ํ๋ด๊ธฐ ์ํด
๋ณ์ state๋ฅผ ํ์ฉํด๋ณด์ธ์.
*/}
{/*
'+', '-' ๋ฒํผ์ ๋๋ฅผ ๋๋ง๋ค state๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒ์
ํ์ธํ ์ ์์ต๋๋ค!
*/}
<h1>{`Count: ${state}`}</h1>
<div>
<button className="plusBtn" onClick={plusNum}>
+
</button>
<button className="minusBtn" onClick={minusNum}>
-
</button>
</div>
</div>
);
}
์ถ์ฒ : codestates
๐ [index.js]
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';
{/*
import { Provider } from 'react-redux';
react-redux์์ Provider๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
*/}
{/*
import { Provider } from 'react-redux';
redux์์ createStore๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
createStore ๋ legacy_createStore๋ก ๋ถ๋ฌ์ฌ ์ ์๋ค.
*/}
import { legacy_createStore as createStore } from 'redux';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
{/*
Action์ ์ด๋ป๊ฒ state๋ฅผ ๋ณ๊ฒฝํ ์ง ์ ์ํด๋์ ๊ฐ์ฒด์
๋๋ค.
Action ๊ฐ์ฒด๋ Dispatch ํจ์๋ฅผ ํตํด
Reducer ํจ์ ๋๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฉ๋๋ค.
*/}
{/*
Action ๊ฐ์ฒด ์์ type์ ํ์๋ก ์ง์ ์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
์ฌ๊ธฐ์ ์ง์ ํ type์ ๋ฐ๋ผ Reducer ํจ์์์ ์๋ก์ด state๋ฅผ ๋ฆฌํดํ๊ฒ ๋ฉ๋๋ค.
*/}
{/*
Action Creator ํจ์ icrease, decrease๋ฅผ ๋ง๋ค์ด ์ฃผ์ธ์.
type์ 'INCREASE', 'DECREASE'๋ก ์ค์ ํฉ๋๋ค.
payload๋ ํน์ ํ ๊ฐ์ ๋ฐ์์ reducer๋ก ์ ๋ฌํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋๋ฐ,
๊ณ ์ ๋ ๊ฐ์ด ์๋ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ณํ ์ ์๋๋ก ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์๋ฅผ ์ ๋ฌ ์ธ์๋ก ๋ฐ์ payload์์ ์ฌ์ฉํฉ๋๋ค.
// payload๊ฐ ํ์ํ ๊ฒฝ์ฐ
const setNumber = (num) => {
return {
type: 'SET_NUMBER',
payload: num
}
}
*/}
{/*
์์ ๋ง๋ Action Creator ํจ์๋ฅผ ๋ค๋ฅธ ํ์ผ์๋ ์ฌ์ฉํ๊ธฐ ์ํด
export๋ฅผ ๋ถํ์ฃผ์ธ์.
*/}
export const increase = () => {
return {
type: 'INCREASE',
};
};
export const decrease = () => {
return {
type: 'DECREASE',
};
};
const count = 1;
{/*
Reducer๋ฅผ ์์ฑํ ๋์๋ ์ด๊ธฐ ์ํ๋ฅผ ์ธ์๋ก ์๊ตฌํฉ๋๋ค.
Reducerํจ์ ์ฒซ๋ฒ์งธ ์ธ์์๋ ๊ธฐ์กด state๊ฐ ๋ค์ด์ค๊ฒ ๋ฉ๋๋ค.
*/}
{/*
์ฒซ๋ฒ์งธ ์ธ์์๋ default value๋ฅผ ๊ผญ ์ค์ ํด์ฃผ์
์ผ ํฉ๋๋ค!
๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ undefined๊ฐ ํ ๋น๋๊ธฐ ๋๋ฌธ์
๊ทธ๋ก ์ธํ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
*/}
{/*
๋๋ฒ์งธ ์ธ์์๋ action ๊ฐ์ฒด๊ฐ ๋ค์ด์ค๊ฒ ๋ฉ๋๋ค.
action ๊ฐ์ฒด์์ ์ ์ํ type์ ๋ฐ๋ผ ์๋ก์ด state๋ฅผ ๋ฆฌํดํฉ๋๋ค.
*/}
const counterReducer = (state=count, action ) => {
{/*
Action ๊ฐ์ฒด์ type ๊ฐ์ ๋ฐ๋ผ ๋ถ๊ธฐํ๋ switch ์กฐ๊ฑด๋ฌธ์
๋๋ค.
*/}
switch (action.type) {
//action === 'INCREASE'์ผ ๊ฒฝ์ฐ
case 'INCREASE':
return state + 1
// action === 'DECREASE'์ผ ๊ฒฝ์ฐ
case 'DECREASE':
return state - 1
// action === 'SET_NUMBER'์ผ ๊ฒฝ์ฐ
case 'SET_NUMBER':
return action.payload
// ํด๋น ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ ๋ ๊ธฐ์กด ์ํ๋ฅผ ๊ทธ๋๋ก ๋ฆฌํด
default:
return state;
}
// Reducer๊ฐ ๋ฆฌํดํ๋ ๊ฐ์ด ์๋ก์ด ์ํ๊ฐ ๋ฉ๋๋ค.
}
{/*
๋ณ์ store์ createStore ๋ฉ์๋๋ฅผ ํตํด store๋ฅผ ๋ง๋ค์ด ์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ , createStore์ ์ธ์๋ก Reducer ํจ์๋ฅผ ์ ๋ฌํด์ฃผ์ด์ผ ํฉ๋๋ค.
(์ง๊ธ ๋จ๊ณ์์๋ ์์์ ํจ์ reducer๋ฅผ ์ ๋ฌํด์ฃผ๊ฒ ์ต๋๋ค.)
*/}
{/*
์๋ก์ด state๋ ์ ์ญ ๋ณ์ ์ ์ฅ์ Store์ ์ ์ฅ๋๊ฒ ๋ฉ๋๋ค.
*/}
const store = createStore(counterReducer);
{/*
Provider๋ store๋ฅผ ์์ฝ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ์ปดํฌ๋ํธ์
๋๋ค.
ํด๋น ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฌ์จ ๋ค์์, Store๋ฅผ ์ฌ์ฉํ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ์ค ํ,
Provider ์ปดํฌ๋ํธ์ props๋ก store๋ฅผ ์ค์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
*/}
{/*
์ ์ญ ์ํ ์ ์ฅ์ store๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ App ์ปดํฌ๋ํธ๋ฅผ
Provider๋ก ๊ฐ์ธ์ค ํ props๋ก ๋ณ์ store๋ฅผ ์ ๋ฌํด์ฃผ์ฌ์ผ ํฉ๋๋ค.
*/}
root.render(
<Provider store={store}>
<App />
</Provider>
);
์ถ์ฒ : codestates
ํ์ฌ๋ index.js์ ๋ชจ๋ ๋ฆฌ๋์ค ์ฝ๋๋ค์ด ์์นํ๊ณ ์๋ค.
์ง๊ธ๊ณผ ๊ฐ์ด ์ฌ๋ฌ ์ญํ ์ ํ๋ ์ฝ๋๋ค์ ํ ๊ณณ์ ์์ฑํ๋ ๊ฒ์ ์ณ์ง ์๋ค.๊ทธ ์ด์ ๋ ๋ํ์ ์ผ๋ก ์ฝ๋์ ๊ฐ๋ ์ฑ์ด ๋งค์ฐ ๋๋น ์ง๊ธฐ ๋๋ฌธ์ด๋ค.
์ฝ๋์ ๊ฐ๋ ์ฑ์ด ๋๋น ์ง๊ฒ ๋๋ฉด ์ค๋ฅ ๋ฐ์ ์ ๋๋ฒ๊น ํ๊ธฐ๊ฐ ๋งค์ฐ ์ด๋ ค์์ง๋ค.๊ทธ๋ฌ๋ฏ๋ก index.js์ ๋ชจ์ฌ์๋ ๋ฆฌ๋์ค ์ฝ๋๋ฅผ ์ญํ ๋ณ๋ก ์ฝ๋๋ฅผ ๋๋๋ ๊ฒ์ด ์ข๋ค.
- Actions
- type์ ๋ฌธ์์ด์ ๋ณ์ํํ ์ด์ ๋ ์ธ๊ฐ์ง ์ด์ ๊ฐ ์๋ค.
- ์คํ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
- ์๋์์ฑ ๊ธฐ๋ฅ์ ํตํด ์ฝ๋ ์์ฐ์ฑ์ ๋์ผ ์ ์๋ค.
- ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ๊ธฐ ์ฉ์ดํ๋ค.
(์์: Action Creator ํจ์, Reducer ํจ์)
- Reducer
- ๋ง์ฝ ์ฌ๋ฌ ๊ฐ์ Reducer๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ,
Redux์ combineReducers ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์
ํ๋์ Reducer๋ก ํฉ์ณ์ค ์ ์๋ค.import { combineReducers } from 'redux'; const rootReducer = combineReducers({ counterReducer, anyReducer, ... });
const count= 1์ธ ์ด๊ธฐ ์ํ
์ด๊ธฐ ์ํ์์
+
๋ฒํผ์ 1๋ฒ ๋๋ ์ ๋
count: 2์ผ ๋,
-
๋ฒํผ์ 1๋ฒ ๋๋ ์ ๋
count: 1์ผ ๋,
-
๋ฒํผ์ 1๋ฒ ๋๋ ์ ๋
์ถ์ฒ : codestates
์ด์ ์งํํ ๊ณผ์ ์ฝ๋๋ฅผ ํ๋ ํ๋ ๋ถ์ํด ๋ณธ๋ค.
๐กโก๏ธ๐ปโก๏ธ๐ค๐
์ค๋์ ํ์ด ์๊ฐ์ ์ข
ํฉํด์ฆ๋ฅผ ๊ฐ์ด ํ์ด๋ณด๊ณ ,
๊ณผ์ ํ์ผ๋ค๊ณผ ์ฝ๋๋ค์ ํ๋์ฉ ์ดํดํด๋ณด์๋ค.
์ต๊ทผ ํ์ต ์ค์ ๊ฐ์ฅ ์ฌ๋ฐ์๊ณ , ์ ์ดํดํ ๊ฒ ๊ฐ์
์ ๋ง ์ฒ์์ผ๋ก ์์
์๊ฐ์ด ๊ธฐ๋ค๋ ค์ก๋ค.
๋ค์์ฃผ์๋ ์น์ ๋ํด ๋ฐฐ์ฐ๋๋ฐ
๋ค์์ฃผ์๋ ์ฌ๋ฐ๊ฒ ์ ๊ณต๋ถํด์ผ๊ฒ ๋ค! ๐คฉ