Ducks pattern 을 구현하는걸 도와준다.
action 의 type을 굳이 상수로 빼서 관리하지 않는다.
createActions() 은 액션 생성 함수가 된다.
import { createActions, createAction, handleActions} from "redux-actions"
log(createAction("HELLO")("안녕"));
func {
type: "HELLO",
payload: "안녕"
}
const actions = createActions('SHOW_ALL', 'SHOW_COMPLETE');
log(actions)
다음과 같이 객체로 감싸진 함수를 return 해 준다.
{
func showAll()
func showComplete()
}
또한 Ducks pattern 에서는 액션의 값에 경로를 넣어주는데 이는 다음과 같이 해주면 된다.
const actions = createActions('SHOW_ALL', 'SHOW_COMPLETE', {prefix: '프로젝트명/파일명'});
구조 분해 할당으로 받을 수 있다.
export { showAll, showComplete } = createActions('SHOW_ALL', 'SHOW_COMPLETE');
// 이게 reducer 가 된다.
export default reducer = handleActions({
// 프로퍼티는 액션의 type
SHOW_ALL: (state, action) => 'ALL',
SHOW_COMPLETE: () => 'COMPLETE'
}, state초기값, {prefix: '프로젝트명/파일명'})
원래는 아래와 같이 작성했었다.
export default function reducer(previousState = initialState, action) {
if (action.type === SHOW_COMPLETE) {
return "COMPLETE";
}
if (action.type === SHOW_ALL) {
return "ALL";
}
return previousState;
}