์ค๋์ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋์ธ Redux๋ฅผ ์ฌ์ฉํด์ Sprint๋ฅผ ๊ณต๋ถํ์ต๋๋ค. ์์ ๋งํ๋ ๊ฒ์ฒ๋ผ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ ์ญ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํด์ฃผ์ด props drilling์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๊ฒ ํด ์ฃผ๋๋ฐ์! ์ด๋ป๊ฒ ๊ด๋ฆฌํ๊ณ ๊ทธ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋์ง์ ๋ํด ์์๋ณผ ์ ์์์ต๋๋ค!
Flux
์๋ ์์๊ณผ ๊ณต์๋ฌธ์๋ฅผ ํตํด Flux์ ๋ํด ๊ณต๋ถํ ์ ์์์ต๋๋ค.
https://facebook.github.io/flux/docs/in-depth-overview/
In-Depth Overview | Flux
Flux is the application architecture that Facebook uses for building client-side web applications. It complements React's composable view components by utilizing a unidirectional data flow. It's more of a pattern rather than a formal framework, and you can
facebook.github.io
Flux๋ ํ์ด์ค๋ถ์์ MVC์ ๋จ์ ์ ๋ณด์ํ๊ธฐ ์ํด ๊ฐ๋ฐํ ์ํคํ ์ฒ์ ๋๋ค.
MVC์ ์๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ผ๋ก ์ธํด ๋ฐ์ํ๋ ์์ธกํ๊ธฐ ์ด๋ ค์ด ๋ฒ๊ทธ๋ค์ผ ๋ํ๋ฉ๋๋ค. ๊ทธ์ค ๋ํ์ ์ธ ์๋ก ์ฑํ ์๋ฆผ ๋ฒ๊ทธ์ธ๋ฐ์. ํ์ด์ค๋ถ์ ๋ก๊ทธ์ธํ์ ๋, ์๋ ์ฌ์ง์ฒ๋ผ ๋ฉ์์ง ์์ด์ฝ์ ์๋ฆผ์ด ๋ ์์ง๋ง ์์ด์ฝ์ ํด๋ฆญํ๋ฉด ์๋ฌด๋ฐ ๋ฉ์์ง๊ฐ ์๋ ๋ฒ๊ทธ๊ฐ ์๊ธธ ๋๊ฐ ์์์ต๋๋ค. ์ด ๋ฒ๊ทธ๋ฅผ ํ๋ฉด์ ์ผ๋ก ์์ ํด์ ์ผ๋ง๊ฐ์ ๊ด์ฐฎ์์ง๋ง, ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๋ฐ์ดํธํ๋ฉด ์๋ฆผ ๋ฒ๊ทธ๊ฐ ๋ค์ ๋ํ๋ฌ์ต๋๋ค. ํ์ด์ค๋ถ์์๋ ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ผ๋ก Flux๋ผ๋ ๊ทผ๋ณธ์ ์ธ ํด๊ฒฐ์ฑ ์ ๋์ต๋๋ค.
Flux์ ์ฅ์ ์ผ๋ก๋ ์๋์ ๊ฐ์ต๋๋ค.
- Improved data consistency
- Easier to pintpoint root of a bug
- More meaningful unit tests
Flux์ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ ์๋ ์ฌ์ง๊ณผ ๊ฐ์ด ์งํ๋ฉ๋๋ค. Action์ Dispatcher๋ฅผ ํตํด Store๋ก, Store์์ View๋ก, View๋ Action์ ํตํด ๋ค์ Dispatcher๋ก ๋ฐ์ดํฐ๊ฐ ํ๋ฅด๊ฒ ๋ฉ๋๋ค. ์ด๋ฌํ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ ๋ฐ์ดํฐ ๋ณํ๋ฅผ ํจ์ฌ ์์ธกํ ์ ์๊ฒ ๋์ด ์์ ๋งํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์์ต๋๋ค.
Dispatcher
Flux์์ ๋ชจ๋ ๋ฐ์ดํฐ์ ํ๋ฆ์ ๊ด๋ฆฌํ๋ ๊ณณ์ ๋๋ค. Action์ ์ ๋ฌํ๋ ๋ฉ์๋์ ๋๋ค. Action์์ ์๋ก์ด action์ ๋ณด๋ด๋ฉด Store๋ dispatcher๋ฅผ ํตํด action์ ๋ฐ๊ฒ ๋ฉ๋๋ค. ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ณต์กํด์ง์๋ก Dispatcher์ ์ค์์ฑ์ ์ปค์ง๋๋ค. ๋ฐ์ดํฐ๋ค ๊ฐ์ ์์กด์ฑ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ด์ฉํ์ฌ MVC๋ณด๋ค ์์ธก ๊ฐ๋ฅํ๊ฒ ๊ด๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
Redux์์๋ Reducer๋ฅผ ํธ์ถํด state ๊ฐ์ ๋ฐ๊พธ๋ ์ญํ ์ ํฉ๋๋ค.
const state = useSelector(state => state.itemReducer);
// useSelector๋ ์ปดํฌ๋ํธ์ state๋ฅผ ์ฐ๊ฒฐํ๋ ์ญํ ์ ํฉ๋๋ค.
// ์ปดํฌ๋ํธ์์ useSelector ๋ฉ์๋๋ฅผ ํตํด store์ state์ ์ ๊ทผํ ์ ์์ต๋๋ค.
const { items, cartItems } = state;
const dispatch = useDispatch();
// useDispatch()๋ Action ๊ฐ์ฒด๋ฅผ Reducer๋ก ์ ๋ฌํด์ฃผ๋ ๋ฉ์๋์
๋๋ค.
// Action์ด ์ผ์ด๋ ๋งํ ๊ณณ์ ํด๋ฆญ ๋ฑ์ ์ด๋ฒคํธ๊ฐ ์ผ์ด๋๋ ์ปดํฌ๋ํธ์
๋๋ค.
Store
Dispatcher์ ์ฝ๋ฐฑ ํจ์๋ฅผ ๋ฑ๋กํ๋ฉด Dispatcher๋ก๋ถํฐ ๋ฉ์ธ์ง๋ฅผ ์์ ๋ฐ์ ์ ์์ต๋๋ค. Store๊ฐ ๋ณ๊ฒฝ๋๋ฉด ๋ณ๊ฒฝ๋ ์ฌ์ค์ View์ ์๋ฆฌ๊ณ , View๋ ์ ์ํ๋ก ์ ๋ฐ์ดํธํ๊ฒ ๋ฉ๋๋ค.
View
View๋ ํ๋ฉด์ ์ ์ํ๋ก ์ ๋ฐ์ดํธํด์ ๋ํ๋ด๊ณ , View Controller์ ์ญํ ๋ ์ํํฉ๋๋ค. Conroller-View๋ ์์ View๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ์ญํ ์ ์ํํฉ๋๋ค.
Action
์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ ๋ ๋ฐ์ดํฐ๊ฐ ๋ด๊ฒจ์๋ ๊ฐ์ฒด๊ฐ ์ธ์๋ก ์ ๋ฌ๋์ด์ผ ํฉ๋๋ค. ์ด ๋ ์ ๋ฌ๋๋ ๊ฐ์ฒด๋ฅผ Action์ด๋ผ๊ณ ํฉ๋๋ค.
์ฆ, ์ด๋ค ์ก์ ์ ์ทจํ ๊ฒ์ธ์ง ์ ์ํด๋์ ๊ฐ์ฒด์ ๋๋ค. ์ฝ๋๋ ์๋์ ๊ฐ์ด ์ด๋ฆ์ด ์ ํ type๊ณผ ์์ฒญ ๊ธฐ๋ฅ์ธ payload๋ก ๊ตฌ์ฑํ์ฌ ์์ฑํ ์ ์์ต๋๋ค.
export const addToCart = (itemId) => {
return {
type: ADD_TO_CART,
payload: {
quantity: 1,
itemId
}
}
}
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TIL 44: [๋ฐฐํฌ] ํด๋ผ์ด์ธํธ ๋น๋์ ๋ฐฐํฌ (0) | 2021.09.30 |
---|---|
TIL43: ๋ณต์ต (0) | 2021.09.23 |
TIL 41: [React] ์ํ ๊ด๋ฆฌ (0) | 2021.09.17 |
TIL 40: [React] ์ปดํฌ๋ํธ ๋์์ธ (0) | 2021.09.15 |
TIL 39: [React] ์ปดํฌ๋ํธ ๋์์ธ (0) | 2021.09.14 |