注意: 如果使用saga的put之類的用法但不在generator function內的話可能會錯誤
<div onClick={() => userLoginEnter()} />
userLoginEnter() {
this.props.userLoginEnterAction({
password: this.state.pass,
groupId: this.state.gid,
userId: this.state.name
});
}
export function userLoginEnter(payload) {
return {
payload,
type: USER_LOGIN_ENTER
};
}
function loginPageReducer(state = initialState, action) {
switch (action.type) {
case USER_LOGIN_ENTER:
return state.set('login', true);
case USER_LOGIN:
sessionStorage.setItem(sessionConst.userInfo, JSON.stringify(action.data));
return state.set('userInfo', action.data);
default:
return state;
}
}
export function* userLoginEnter(action) {
try {
const payload = yield new Promise(resolve => {
callApi('post', 'login', {
password: action.payload.password,
groupId: action.payload.groupId,
userId: action.payload.userId,
simpleLogin: false,
appId: "we6"
}, (response) => { resolve(response); });
});
yield put(userLogin(payload));
} catch (err) {
console.error(`userLogin saga error, err msg => ${err}`);
}
}
export function* watchUserLogin() {
yield takeLatest(USER_LOGIN_ENTER, userLoginEnter);
}
export default [
watchUserLogin
];
sagaMiddleware.run(watchUserLogin);