let's get IT with DAVINA ๐ป
Redux ๋ณธ๋ฌธ
Redux๋?
- ์ํ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์ ์ญ ์ํ๋ฅผ ๊ด๋ฆฌํ ์ ์๋ Store์ ์ ๊ณตํจ์ผ๋ก์จ ํ๊ธฐ์ ๊ฐ์ ๋ฌธ์ ํด๊ฒฐ
- ์ปดํฌ๋ํธ์ ์ํ๋ฅผ ๋ถ๋ฆฌํ๋ ํจํด
Redux์ ๊ธฐ๋ณธ ๊ตฌ์กฐ
- ์ํ๊ฐ ๋ณ๊ฒฝ๋์ด์ผ ํ๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด, ๋ณ๊ฒฝ๋ ์ํ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด Action ๊ฐ์ฒด๊ฐ ์์ฑ๋ฉ๋๋ค.
- ์ด Action๊ฐ์ฒด๋ Dispatch ํจ์์ ์ธ์๋ก ์ ๋ฌ๋ฉ๋๋ค.
- Dispatch ํจ์๋ Action ๊ฐ์ฒด๋ฅผ Reducer ํจ์๋ก ์ ๋ฌํด์ค๋๋ค.
- Reducerํจ์๋ Action ๊ฐ์ฒด์ ๊ฐ์ ํ์ธํ๊ณ , ๊ทธ ๊ฐ์ ๋ฐ๋ผ ์ ์ญ ์ํ ์ ์ฅ์ Store์ ์ํ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
- ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ฉด, React๋ ํ๋ฉด์ ๋ค์ ๋ ๋๋ง ํฉ๋๋ค.
์ฆ, Redux์์ Action→Dispatch→Reducer→Store ์์๋ก ๋ฐ์ดํฐ๊ฐ ๋จ๋ฐฉํฅ์ผ๋ก ํ๋ฅด๊ฒ ๋ฉ๋๋ค.
redux ์ค์น
npm install redux
1. store ์์ฑ
store = date๋ฅผ ์ ์ฅํ๋ ๊ณณ
import {createStore} from "redux"
const store=createStore();
2. reducer ์์ฑ
1๋ฒ ๊ณผ์ ์์ createStore์ reducer ํจ์๊ฐ ํ์ํ๋ค๋ error๊ฐ ๋ธ!
reducer = data๋ฅผ modifyํด์ฃผ๋ ํจ์๋ก, reducer๊ฐ returnํ๋ ๊ฐ์ด application์ ์๋ data๊ฐ ๋จ
import {createStore} from "redux"
const reducer = () => {};
const store = createStore(reducer);
3. action ์์ฑ
reducerํจ์๊ฐ ์ด๋ป๊ฒ data๋ฅผ ๋ณ๊ฒฝํ ์ง ๋ฐฉ๋ฒ์ ์ทจํด์ฃผ๋ ๊ฐ์ฒด
{type: “ADD”}ํํ๋ก ๊ฐ์ฒด์ฌ์ผ ํจ!!
import {createStore} from "redux"
const reducer = (count=0, action) => { //undefined์ด๊ธฐ ๋๋ฌธ์ ์ด๊ธฐ ๋ฐ์ดํฐ๊ฐ ์ง์
if(action.type==="ADD") {
return count+1;
} else if(action.type==="MINUS"){
return count-1;
} else {
return count
}
};
const store = createStore(reducer);
//refactoring
import {createStore} from "redux"
const ADD = "ADD"; //์ค์๋ก ์คํ๋ฅผ ๋ผ ์, ์๋ฌ๋ฅผ ๋์ฐ๊ธฐ ์ํด ๋ณ์๋ก ์ ์ธ
const MINUS = "MINUS";
const reducer = (count=0, action) => { //undefined์ด๊ธฐ ๋๋ฌธ์ ์ด๊ธฐ ๋ฐ์ดํฐ๊ฐ ์ง์
switch(action.type){
case ADD:
return count+1;
case MINUS:
return count-1;
default:
return count;
}
};
const store = createStore(reducer);
4. dispatch ์์ฑ
reducerํจ์์๊ฒ action๊ฐ์ฒด๋ฅผ ์ ๋ฌํด์ค์ communicateํ๊ฒ ํด์ฃผ๋ ํจ์
import {createStore} from "redux"
const reducer = (count=0, action) => {
if(action.type==="ADD") {
return count+1;
} else if(action.type==="MINUS"){
return count-1;
} else {
return count
}
};
const store = createStore(reducer);
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"ADD"});
store.dispatch({type:"MINUS"});
console.log(store.getState()); //4๊ฐ ์ถ๋ ฅ๋จ
5. subscribe ์์ฑ
store ์์ ์๋ ๋ณํ ๊ฐ์ง
const onChange = () => {
number.innerText = store.getState();
}
store.subscribe(onChange);
Redux Hooks
- Store, Reducer, Action, Dispatch ๊ฐ๋ ๋ค์ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ญํ
- React-Redux์์ Redux๋ฅผ ์ฌ์ฉํ ๋ ํ์ฉํ ์ ์๋ Hooks ๋ฉ์๋ ์ ๊ณต
- useSelector()
- useDispatch()
useDispatch()
useDispatch()๋ Action ๊ฐ์ฒด๋ฅผ Reducer๋ก ์ ๋ฌํด์ฃผ๋ Dispatch ํจ์๋ฅผ ๋ฐํํ๋ method์ ๋๋ค. (์ํ ๋ณํ)
import { useDispatch } from 'react-redux'
const dispatch = useDispatch()
dispatch( increase() )
console.log(counter) // 2
dispatch( setNumber(5) )
console.log(counter) // 5
useSelector()
useSelector()๋ ์ปดํฌ๋ํธ์ state๋ฅผ ์ฐ๊ฒฐํ์ฌ Redux์ state์ ์ ๊ทผํ ์ ์๊ฒ ํด์ฃผ๋ method์ ๋๋ค. (์ํ๊ฐ ์กฐํ)
// Redux Hooks ๋ฉ์๋๋ 'redux'๊ฐ ์๋๋ผ 'react-redux'์์ ๋ถ๋ฌ์ต๋๋ค.
import { useSelector } from 'react-redux'
const counter = useSelector(state => state)
console.log(counter) // 1
[์๊ณ ํธ]
Redux Toolkit ? → Redux ์ฝ๋์์ ์ค์ด๊ธฐ ์ข์ ํจํค์ง
'DEV_IN > Library' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Redux Toolkit (4) | 2023.02.21 |
---|