티스토리 뷰
Ducks: Redux Reducer Bundles
Redux를 사용하여 상태관리를 구축하는 과정에서 하나의 기능을 만들 떄 각각 use case에 따라 type, action, reducer를 계속 추가해야 된다는 점과 reducer와 actions만 서로 연관된 동작을 하므로 하나의 독립된 모듈로 묶는것이 더 의미가 있고 라이브러리로 packaging하기 더 쉽다고 한다.
규칙
1. 하나의 모듈은 항상 reducer 함수를 default로 export해야 한다.
2. action 생성자를 함수로 export해야 한다.
3. action 타입은 npm-module-or-app/reducer
4. 외부 reducer가 해당 action들의 발생을 계속 기다리거나, 재사용이 가능한 라이브러리일 경우 "UPPER_SNAKE_CASE"로 export 할 수 있다.
typescript, redux-action라이브러리를 사용한 작성한 간단한 integer 증감 상태 관리 모듈이다.
import { handleActions, createAction } from 'redux-actions';
const INCREASE = 'counter/INCREASE';
const DECREASE = 'counter/DECREASE';
const INCREASE_BY = 'counter/INCREASE_BY';
export const increase = createAction(INCREASE);
export const decrease = createAction(DECREASE);
export const increase_by = createAction<number>(INCREASE_BY);
type CounterAction =
ReturnType<typeof increase> |
ReturnType<typeof decrease> |
ReturnType<typeof increase_by>;
export interface CounterState{
count: number;
}
export const initialCounterState: CounterState = {
count: 0
}
export default handleActions<CounterState, CounterAction>({
[INCREASE]: (state: CounterState) => ({
...state,
count: state.count + 1
}),
[DECREASE]: (state: CounterState) => ({
...state,
count: state.count - 1
}),
[INCREASE_BY]: (state: CounterState, action: CounterAction) => ({
...state,
count: state.count + action.payload
})
}, initialCounterState);
https://github.com/JisuPark/ducks-modular-redux
JisuPark/ducks-modular-redux
A proposal for bundling reducers, action types and actions when using Redux - JisuPark/ducks-modular-redux
github.com
'front-end > react' 카테고리의 다른 글
React Diffing algorithm1 - Heuristics algorithm (0) | 2021.12.13 |
---|---|
Next.js 맛보기 (0) | 2021.08.02 |
[React] react hooks와 closure의 관계 (0) | 2021.06.23 |
Redux-saga로 웹 소켓 통신 구현 (0) | 2021.06.13 |
[React] react router (0) | 2021.06.04 |