반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 시스템설계면접예시
- 전략패턴
- gitsquash
- 시스템설계면접팁
- 시스템설계
- cypressBDD
- git squash
- cypress React
- git commit merge
- 디자인패턴
- 가상면접3장
- 리팩토링2판4장
- react
- 헤드퍼스트전략패턴
- git commit 협업
- s3이미지다운로드됨
- 리액트구글애널리틱스
- formik submitting not working
- 시스템설계방법
- 가상면접2장
- Git commit 합치기
- 가상면접으로대규모시스템
- FirebaseAnalytics
- 시스템설계면접
- formik react-query submitting not working
- 테스트코드책
- file not found Error
- react-ga
- 리팩터링2판테스트
- awss3
Archives
- Today
- Total
mingg IT
[React] redux-action 사용하기 (typeScript) 본문
리덕스를 좀 더 편하게 사용하는 방법이다. redux-actions를 사용하면 액션 생성 함수를 훨씬 짧고 가독성 좋은 함수로 만들수 있다.
1. yarn add redux-actions
typeScript를 사용할 경우에는 (npm i --save-dev @types/redux-actions)
2. 사용전 코드
export const changeInput = (input: String) => ({
type: CHANGE_INPUT,
input,
});
function search(state = initialState, action: any) {
switch (action.type) {
case CHANGE_INPUT:
return {
...state,
input: action.input,
};
default:
return state;
}
}
3. 사용 후 코드
import { createAction, handleActions } from "redux-actions";
export const changeInput = createAction(CHANGE_INPUT, (input: String) => input);
const search = handleActions(
{
[CHANGE_INPUT]: (state, { payload }) => ({
...state,
input: payload.input,
}),
},
initialState
);
search 함수가 훨신 짧아졌다. 지금은 예시가 1개 뿐이지만 사용전 코드에서는 switch, case를 쓰며 주렁주렁 길어지지만 지금은 훨씬더 짧고 가독성 있게 바꿀 수 있다.
'FrontEnd' 카테고리의 다른 글
Comments