const gen = function*(){
console.log("1")
yield
console.log("2")
}
- const generator=gen()
1. generator.next() => 1
2. generator.next() => 2
const gen=function*(){
while(true){
yield "무한"
}
}
- const generator=gen()
1. generator.next() => "무한" -> 무한반복 안됨
thunk(프로그래밍 용어) : 지연된 함수
한번에 디스패치를 여러번 할 수 있도록 해준다.
thunk 깃헙소스(dispatch 안 dispatch가 가능)
export const loginAction=(data)=>{
return(dispatch)=>{
dispatch(loginRequestAction());
try {
dispatch(loginSuccessAction());
}
catch ((err)=>{
dispatch(loginFailureAction(err))
})
}
}
yield all([fork(watchLogin), fork(watchLogOut), fork(watchAddPost)]);
const result = yield call(loginAPI, action.data);
-> 장점 : 테스트하기편함
// const l = logIn({ type: 'LOG_IN_REQUEST', data: { id: 'id' } });
// l.next()
yield put({
type: 'LOG_IN_FAILURE',
data: err.response.data,
});
import produce from 'immer';
...
const reducer = (state = initialState, action) => {
return produce(state, (draft) => {
const post = draft.mainPosts.find(
(v) => v.id === action.data.postId
);
post.Comments.unshift(dummyComment(action.data.content));
draft.addCommentLoading = false;
draft.addCommentDone = true;
break;
default:
break;
}
});
...
const configureStore = () => {
const sagaMiddleware = createSagaMiddleware();
const middlewares = [sagaMiddleware, loggerMiddleware];
const enhancer =
process.env.NODE_ENV === 'production'
? compose(applyMiddleware(...middlewares))
: composeWithDevTools(applyMiddleware(...middlewares));
const store = createStore(reducer, enhancer);
store.sagaTask = sagaMiddleware.run(rootSaga);
store.dispatch({
type: 'CHANGE_NICKNAME',
data: '100dong',
});
return store;
};
다시 알아보세요