ํฐ์คํ ๋ฆฌ ๋ทฐ
[โ๏ธ] Redux - ์๊ฐ ๋ฐ ์ค์ + counter ์ฑ ์ธํ (store ๋ง๋ค๊ธฐ)
์ฑ._. 2025. 2. 1. 00:20react์์ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ด๋ผ๊ณ ํ ์ ์๋ redux.
redux๊ฐ ๋ฌด์์ธ์ง, ํฐ ํ๋ฆ์ ์ด๋ป๊ฒ ๋๋์ง ์ ๋ฆฌํด๋ณด๊ฒ ๋ค.
redux๋?
๐์ฌ์ฉ ์ด์
- useState์ ๋ถํธํจ
- props๋ฅผ ํตํด์ ๋จ๋ฐฉํฅ(๋ถ๋ชจ -> ์์)์ผ๋ก์ state ์ ๋ฌ๋ง ๊ฐ๋ฅ
- ์กฐ๋ถ๋ชจ -> ์์ ์ฌ์ด์ ๋ฐ๋์ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ฅผ ๊ฑฐ์ณ์ผ ํจ.
๐์ ์
- “์ค์ state ๊ด๋ฆฌ์”๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋์์ฃผ๋ ํจํค์ง(๋ผ์ด๋ธ๋ฌ๋ฆฌ)
- ์ปดํฌ๋ํธ ๋ฐ์์ ๋ฐ๋ก ์์ฑ
- ์ปดํฌ๋ํธ์ ์์น ์๊ด์์ด state ์ฌ์ฉ ๊ฐ๋ฅ
- Context API ๋ณด๋ค ๊ท๋ชจ๊ฐ ํฐ state์์ ์ ์ฉ

counter ์ฑ ์ธํ
ํฌ๊ฒ ๋ค ๋จ๊ณ๋ก ๋๋ ์ ์๋ค.
1. ๋ฆฌ๋์ค ์ค์
1-1. ๋ฆฌ๋์ค ์ค์น
1-2. ํด๋ ๊ตฌ์กฐ ์์ฑ
2. ์ค์ ์ฝ๋ ์์ฑ
2-1. configStore.js
2-2. store ์ฃผ์ (main.jsx)
3. ๋ชจ๋ ๋ง๋ค๊ธฐ
3-1. reducer ํจ์ ๋ง๋ค๊ธฐ
3-2. ๋ชจ๋์ ๊ตฌ์ฑ์์
3-3. ์นด์ดํฐ ๋ชจ๋์ store์ ์ฐ๊ฒฐ
4. store์ ๋ชจ๋ ์ฐ๊ฒฐ ํ์ธ
1. ๋ฆฌ๋์ค ์ค์
๐1-1. ๋ฆฌ๋์ค ์ค์น
๊ธฐ์กด react vite ์ค์น ๋ฐฉ๋ฒ
[ํฐ๋ฏธ๋]
yarn create vite ํ๋ก์ ํธ๋ช --template react
+ redux ํ๋ก์ ํธ ์์ฑ
yarn add redux react-redux
# ์๋์ ๊ฐ์ ์๋ฏธ
yarn add redux
yarn add react-redux
๐1-2. ํด๋ ๊ตฌ์กฐ ์์ฑ

- src
- redux
- config (์ค์ ๋ฐ์ดํฐ ๊ด๋ฆฌ์:store)
- configStore.js
- modules(state๋ค์ ๊ทธ๋ฃน)
- counter.js
- config (์ค์ ๋ฐ์ดํฐ ๊ด๋ฆฌ์:store)
- redux
2. ์ค์ ์ฝ๋ ์์ฑ
๐2-1. configStore.js ์์ store ๋ง๋ค๊ธฐ
- combineReducers() : rootReducer ๋ง๋ค๊ธฐ
- createStore() : store๋ฅผ ๋ง๋๋ ๋ฉ์๋(ํจ์)
- store export
import { createStore } from "redux";
import { combineReducers } from "redux";
const rootReducer = combineReducers({});
const store = createStore(rootReducer);
export default store;
๐2-2. main.jsx์ store ์ฃผ์ ํ๊ธฐ
- App์ Provider๋ก ๊ฐ์ธ์ ์ด ์์ญ ๋ด์์๋ store๋ฅผ ์ฌ์ฉํ ์ ์์
- ์์์ ๋ง๋ store ์ฃผ์
<Provider store={store}>
<App />
</Provider>
3. ๋ชจ๋ ๋ง๋ค๊ธฐ
๋ฆฌ๋์ค๋ฅผ ํตํด ์นด์ดํฐ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด ๋ณธ๋ค.
๐3-1. reducer ํจ์ ๋ง๋ค๊ธฐ
- input์๋ state = ์ด๊ธฐ๊ฐ, action
- ๋ก์ง์๋ switch๋ฌธ (key: action.type, state ์์ฒด๋ฅผ return)
// src/redux/modules/counter.js
// ์ด๊ธฐ ์ํ๊ฐ
const initialState = {
number: 0,
};
// ๋ฆฌ๋์
const counter = (state = initialState, action) => {
switch (action.type) {
default:
return state;
}
};
// ๋ชจ๋ํ์ผ์์๋ ๋ฆฌ๋์๋ฅผ export default ํ๋ค.
export default counter;
๐3-2. ๋ชจ๋์ ๊ตฌ์ฑ์์
- initialState === ์ด๊ธฐ ์ํ๊ฐ
- ๊ฐ์ฒด์ ์ฌ๋ฌ ๊ฐ์ ๋ณ์๋ฅผ ๋ฃ์ด์ค ์ ์์
- ์ด๊ธฐ ์ํ๊ฐ์ ๊ฐ์ฒด ์ธ์๋ ๋ฐฐ์ด, ์์๋ฐ์ดํฐ๊ฐ ๋ ์ ์์
// ์ด๊ธฐ ์ํ๊ฐ
const initialState = {
number: 0,
};
- Reducer === ๋ณํ๋ฅผ ์ผ์ผํค๋ ํจ์
- reducer๋ ๋ณํ๋ฅผ ์ผ์ผํค๋ ํจ์๋ค.(= useState์์ setState์ ์ญํ )
- state์ ์ด๊ธฐ๊ฐ์ ํ ๋นํด์ผ ํจ
// ๋ฆฌ๋์
const counter = (state = initialState, action) => {
switch (action.type) {
default:
return state;
}
};
๐3-3. ์นด์ดํฐ ๋ชจ๋์ store์ ์ฐ๊ฒฐ
- configStore.js ๋ก ์ด๋ํ์ฌ ์ฝ๋ ์ถ๊ฐ(2-1 ๋ก์ง ์ฐธ๊ณ )
// src/redux/config/configStore.js
// ์๋ ์๋ ์ฝ๋
import { createStore } from "redux";
import { combineReducers } from "redux";
// ์๋กญ๊ฒ ์ถ๊ฐํ ๋ถ๋ถ
import counter from "../modules/counter";
const rootReducer = combineReducers({
counter: counter, // <-- ์๋กญ๊ฒ ์ถ๊ฐํ ๋ถ๋ถ
});
const store = createStore(rootReducer);
export default store;
4. store์ ๋ชจ๋ ์ฐ๊ฒฐ ํ์ธ
- App.jsx ์ปดํฌ๋ํธ์์ useSelector ํ ์ฌ์ฉํ๊ธฐ
// 1. store์์ ๊บผ๋ธ ๊ฐ์ ํ ๋น ํ ๋ณ์๋ฅผ ์ ์ธ
const number =
// 2. useSelector()๋ฅผ ๋ณ์์ ํ ๋น
const number = useSelector()
// 3. useSelector์ ์ธ์์ ํ์ดํ ํจ์๋ฅผ ๋ฃ์ด์ฃผ๊ธฐ
const number = useSelector( ()=>{} )
// 4. ํ์ดํ ํจ์์ ์ธ์์์ ๊ฐ์ ๊บผ๋ด return
// useSelector๋ฅผ ์ฒ์ ์ฌ์ฉํด๋ณด๋ ๊ฒ์ด๋, state๊ฐ ์ด๋ค ๊ฒ์ธ์ง ์ฝ์๋ก ํ์ธํ๊ธฐ!
const number = useSelector((state) => {
console.log(state)
return state
});
configStore.js์์ store๋ฅผ ์์ฑํ๊ณ ,
main.jsx์ store๋ฅผ ์ฃผ์ ํ๊ณ ,
module์ ๋ง๋ค๋ฉฐ reducer ํจ์๋ฅผ ์์ฑํ๋ค.
์ด๋ ๊ฒ ๋ง๋ reducer์๊ฒ ์ด๋ป๊ฒ ๋ช ๋ น์ ์ ๋ฌํ ์ง,
reducer๋ ๋ฐ์ ๋ช ๋ น์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง์ ์ง์คํ์ฌ ๋ค์ ๋จ๊ณ๋ฅผ ์๊ฐํด๋ด์ผ ํ๋ค.
'Language > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Pokemon PJ_Day 4] RTK (1) - ์ธํ ํ๊ธฐ (0) | 2025.02.06 |
|---|---|
| [โ๏ธ] Redux - counter ์ฑ ์์ฑ, redux ์ฌ์ดํด, dispatch (0) | 2025.02.03 |
| [โ๏ธ] custom hook (0) | 2025.01.30 |
| [โ๏ธ] memoization (0) | 2025.01.30 |
| [โ๏ธ] useContext (0) | 2025.01.30 |