๐ธ redux-actions
yarn add redux-actions
- ์ก์ ์์ฑ ํจ์๋ฅผ ๋ ์งง์ ์ฝ๋๋ก ์์ฑํ ์ ์๊ฒ ํด์ค๋ค.
- ๋ฆฌ๋์๋ฅผ ์์ฑํ ๋ switch๋ฌธ์ด ์๋ handleActions๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค.
๐ธ createAction
- ์ก์ ์์ฑ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์์ด๋ค.
- ์ง์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ํ์ ์์ด ํจ์ฌ ๊ฐ๋จํ๋ค.
createAction์ ์ฌ์ฉํ์ง ์์์ ๋
const CHANGE_USER = 'user/CHANGE_USER';
// ์ก์
์์ฑ ํจ์
export const change_user = user => ({type: CHANGE_USER, user});
createAction์ ์ฌ์ฉํ์ ๋
import { createAction } from 'redux-actions';
const CHANGE_USER = 'user/CHANGE_USER';
// ์ก์
์์ฑ ํจ์
export const change_user = createAction(CHANGE_USER, user => user);
๐ธ handleActions
- handleActions๋ก ๋ฆฌ๋์๋ฅผ ๋ ๊ฐ๋จํ๊ฒ ์์ฑํ ์ ์๋ค.
handleActions์ ์ฌ์ฉํ์ง ์์์ ๋
const reducer = (state = initState, action) => {
switch (action.type) {
case CHANGE_USER:
return {
...state,
user: action.user
}
}
}
handleActions์ ์ฌ์ฉํ์ ๋
import { handleActions } from 'redux-actions';
const reducer = handleActions({
[CHANGE_USER]: (state, action) => ({...state, user: action.user})
});
๋ฐ์ํ
'react > redux' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] redux-saga ์์ํ๊ธฐ! ๊ธฐ๋ณธ์ ์ธ effects ํ์ฉ (0) | 2021.03.31 |
---|---|
[React] useSelector, useDispatch๋ก state์ ์ ๊ทผํ๊ธฐ (0) | 2021.03.31 |
[React] ๋ฆฌ๋์ค ์์ํ๊ธฐ (redux, react-redux) (0) | 2021.03.31 |