Mor*_*eza 2 reactjs react-native redux redux-saga react-redux
我需要将动态数据从屏幕发送到动作/减速器,并使用该数据从API中获取数据,但是当我在rootSaga中屈服时,会出现如下错误:
在check take(patternOrChannel)时
未捕获:patternOrChannel未定义在takeSever的rootSaga处的rootSaga
处未捕获patternEvery
错误:take(patternOrChannel):patternOrChannel未定义
屏幕代码:
import { checkUserLoginStatus, userSignin } from '../actions/user';
class PreCheckout extends PureComponent {
handleLogin = () => {
this.props.dispatch(userSignin(username, password));
};
render() { .......
Run Code Online (Sandbox Code Playgroud)
行动:
const USER_SIGNIN = 'USER_SIGNIN';
export const userSignin = (username, password) => ({
type: USER_SIGNIN,
username,
password,
});
Run Code Online (Sandbox Code Playgroud)
减速器:
import {
CHECK_USER_LOGIN_STATUS,
USER_SIGNIN,
USER_SIGNIN_RESULT,
USER_SIGNIN_ERROR,
} from '../actions/user';
const initialState = {
isLoggedIn: false,
isFetching: false,
information: {},
error: null,
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case CHECK_USER_LOGIN_STATUS:
return {
...state,
};
case USER_SIGNIN:
return {
...state,
isFetching: true,
};
case USER_SIGNIN_RESULT:
return {
...state,
isFetching: false,
information: action.result,
};
case USER_SIGNIN_ERROR:
return {
...state,
isFetching: false,
error: action.error,
};
Run Code Online (Sandbox Code Playgroud)
Redux-Saga:
import {
USER_SIGNIN,
USER_SIGNIN_RESULT,
USER_SIGNIN_ERROR,
} from '../actions/user';
function* fetchUserInformation(action) {
try {
console.log('fetchUserInformation action: ', action);
const response = yield call(login, action);
yield put({
type: USER_SIGNIN_RESULT,
result: response.result,
});
} catch (e) {
yield put({
type: USER_SIGNIN_ERROR,
error: e.message,
});
}
}
export default function* rootSaga() {
yield takeEvery(USER_SIGNIN, fetchUserInformation);
}
Run Code Online (Sandbox Code Playgroud)
正如我在评论中提到的。您只是忘了导出常数。
应该
export const USER_SIGNIN = 'USER_SIGNIN';
Run Code Online (Sandbox Code Playgroud)
要么
const USER_SIGNIN = 'USER_SIGNIN';
...
export { USER_SIGNIN };
Run Code Online (Sandbox Code Playgroud)
这些类型的错误可以被捕获eslint使用eslint-plugin-import这个规则启用。
| 归档时间: |
|
| 查看次数: |
3926 次 |
| 最近记录: |