์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Design Pattern
- skt membership
- ์๋ผํ ์คํ ๋ค์ค์ ์ฒด
- Prefix Sums
- c++
- dataframe
- ๊ฐํํ์ต
- ์์
- margin
- pandas
- Photoshop
- Codility
- ํ๋ฅ
- spring
- grid
- Javascript
- ๋ฐฑ์ค
- align-items
- ์๊ณ ๋ฆฌ์ฆ
- CSS
- series
- ์ํ
- Gap
- ํต์ ์ฌํ ์ธ
- ์ํ
- c
- ํฌํ ์ต
- Flexbox
- SK๋ฐ์ด์ค์ฌ์ด์ธ์ค
- stl
- Today
- Total
sliver__
[React] Context + useReducer ๋ณธ๋ฌธ
๐ง React์ ์ํ ๊ด๋ฆฌ๋ฅผ Context + useReducer๋ก ์ฝ๊ฒ ํด๋ณด์!
React๋ฅผ ์ฌ์ฉํ๋ค ๋ณด๋ฉด, ์ปดํฌ๋ํธ ๊ฐ ์ํ ๊ณต์ ๊ฐ ํ์ํด์ง๋ ์๊ฐ์ด ์ฐพ์์ต๋๋ค. ์ด๋ด ๋ ํํ props drilling
๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฃ . ์ํ๋ฅผ ์์์ ์์๊น์ง ๊ณ์ ๋๊ฒจ์ค์ผ ํ๋๊น์.
๊ทธ๋ด ๋ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด ๋ฐ๋ก Context API์
๋๋ค. ํ์ง๋ง Context๋ง์ผ๋ก ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ ค๋ฉด ์ฝ๋๊ฐ ๋ณต์กํ๊ณ ๊ด๋ฆฌ๊ฐ ์ด๋ ค์์ง๊ธฐ๋ ํด์. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด useReducer
๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ฉด ์์ฃผ ๊น๋ํ๊ฒ ํด๊ฒฐํ ์ ์์ต๋๋ค.
๐ Context API + useReducer ์กฐํฉ์ด๋?
Context API
๋ ์ ์ญ ์ํ๋ฅผ ๋ง๋ค๊ณ ์ฑ ์ ์ฒด์ ๊ณต์ ํ ์ ์๊ฒ ํด์ค๋๋ค.useReducer
๋ ๋ณต์กํ ์ํ ์
๋ฐ์ดํธ๋ฅผ switch-case
๋ก ๋ช
ํํ๊ฒ ๊ด๋ฆฌํ ์ ์๋๋ก ๋์์ค๋๋ค.
โ Context API๋ ์ ์ญ ์ํ ๊ณต๊ฐ์ ๋ง๋๋ ์ญํ
โ useReducer๋ ๊ทธ ๊ณต๊ฐ์ ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ์ ์ํ๋ ๋ก์ง
๐ฆ ํ๋ก์ ํธ ๊ตฌ์กฐ ์์
src/ โโโ context/ โ โโโ CounterContext.js โโโ components/ โ โโโ Counter.js โโโ App.js
๐ง Step 1: Context + Reducer ์ ์
// src/context/CounterContext.js
import React, { createContext, useReducer, useContext } from 'react';
const initialState = { count: 0 };
const counterReducer = (state, action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
case 'RESET':
return { count: 0 };
default:
throw new Error(`Unhandled action type: ${action.type}`);
}
};
const CounterContext = createContext();
export const CounterProvider = ({ children }) => {
const [state, dispatch] = useReducer(counterReducer, initialState);
return (
{children}
);
};
export const useCounter = () => {
const context = useContext(CounterContext);
if (!context) {
throw new Error('useCounter must be used within a CounterProvider');
}
return context;
};
๐งฑ Step 2: Provider๋ก ์ฑ ๊ฐ์ธ๊ธฐ
// App.js
import React from 'react';
import { CounterProvider } from './context/CounterContext';
import Counter from './components/Counter';
function App() {
return (
Context + useReducer Example
);
}
export default App;
๐ Step 3: ์ํ๋ฅผ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ
// src/components/Counter.js
import React from 'react';
import { useCounter } from '../context/CounterContext';
const Counter = () => {
const { state, dispatch } = useCounter();
return (
Count: {state.count}
);
};
export default Counter;
โ ์ฅ์ ์์ฝ
์ฅ์ | ์ค๋ช |
---|---|
๊ฐ๋ณ๊ณ ๋น ๋ฆ | ๋ณ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด React๋ง์ผ๋ก ์ ์ญ ์ํ ๊ด๋ฆฌ ๊ฐ๋ฅ |
๊ฐ๋ ์ฑ | ์ํ ๋ก์ง์ด reducer์ ๋ชจ์ฌ ์์ด ๊ด๋ฆฌ๊ฐ ์ฌ์ |
๋ชจ๋ํ ์ฌ์ | Context ๋จ์๋ก ์๊ฒ ์ชผ๊ฐ์ ๊ธฐ๋ฅ๋ณ ๊ด๋ฆฌ ๊ฐ๋ฅ |
ํ ์คํธ ์ฉ์ด | reducer ํจ์๋ ์์ ํจ์๋ผ ํ ์คํธ๊ฐ ๊ฐ๋จํจ |
โ ๏ธ ์ฃผ์ํ ์
- ๋ชจ๋ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง ๋ ์ ์์
Context ๊ฐ์ ๊ตฌ๋ ํ๋ ์ปดํฌ๋ํธ๋ Provider ๊ฐ์ด ๋ฐ๋๋ฉด ๋ฌด์กฐ๊ฑด ๋ฆฌ๋ ๋๋ฉ๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ: Context๋ฅผ ์ชผ๊ฐ๊ฑฐ๋useMemo
,useContextSelector
๋ฑ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ด์. - ๋น๋๊ธฐ ๋ก์ง์ ๋ฐ๋ก ์ฒ๋ฆฌํด์ผ ํจ
Redux์ฒ๋ผ ๋ฏธ๋ค์จ์ด๊ฐ ์๊ธฐ ๋๋ฌธ์, ๋น๋๊ธฐ ๋ก์ง์useEffect
๋๋ ์ปค์คํ ํ ์ผ๋ก ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค.
๐งฉ ๋ง๋ฌด๋ฆฌ
Context + useReducer๋ React์์ ์ ์ญ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํ ๊ฐ๊ฒฐํ๋ฉด์๋ ๊ฐ๋ ฅํ ์กฐํฉ์
๋๋ค.
Redux๋ฅผ ๋์
ํ๊ธฐ์ ๋ฌด๊ฑฐ์ด ๊ฒฝ์ฐ, ์ด ์กฐํฉ์ ์ฌ์ฉํ๋ฉด ํจ์ฌ ๋จ์ํ๊ณ ์ง๊ด์ ์ธ ์ฝ๋๋ก ์ํ๋ฅผ ๊ด๋ฆฌํ ์ ์์ด์.
๐ ์ค์ํ ํ๋ก์ ํธ, ๊ฐ๋จํ ์ํ ๊ณต์ ๊ฐ ํ์ํ ์ฑ์์ ์์ฃผ ์ ์ฉํ๊ฒ ์ธ ์ ์์ต๋๋ค.
์์ผ๋ก ์ํ๊ฐ ์ ์ ๋ณต์กํด์ง๊ฑฐ๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์๊ตฌ๊ฐ ๋ง์์ง๋ค๋ฉด, Redux์ ๊ฐ์ ์ ๋ฌธ ์ํ ๊ด๋ฆฌ ํด๋ก์ ์ ํ๋ ๊ณ ๋ คํด๋ณด์ธ์.
ํ์ง๋ง ๊ทธ ์ ๊น์ง๋ Context + useReducer ์กฐํฉ์ด๋ฉด ์ถฉ๋ถํ ๊ฐ๋ ฅํฉ๋๋ค ๐ช
'Frontend > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] useCallback (0) | 2025.04.11 |
---|---|
[React] useEffect (0) | 2025.04.11 |
[React] Context API + useReducer (0) | 2025.04.06 |
[React] useRef props (react 18 vs 19) (0) | 2025.02.16 |
[React] useRef (0) | 2025.02.16 |