TypeError:未定义不是一个对象(评估'_UserInfoRedux.UserInfoActions.userInfoRequest')
2020-02-21
1710
我是 React Native 的新手,我正在使用 Redux-Saga。不幸的是,有一个我无法解决的错误。
我在调度操作时收到此错误:
TypeError: undefined is not an object (evaluating '_UserInfoRedux.UserInfoActions.userInfoRequest')
这是我调度操作的组件:
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {UserInfoActions } from '../Redux/UserInfoRedux'
class LoginScreen extends Component {
constructor(props){
super(props)
this.state = {
username: '', password: '', rememberPwd:false
}
this.props.getUserInfo();
}
componentWillReceiveProps(newProps) {
this.forceUpdate();
if (newProps.userInfo.error === null && newProps.userInfo.payload !== null) {
console.log(newProps.userInfo.payload.results);
} else {
console.log(">> error");
}
}
render() {
.
.
.
}
}
const mapStateToProps = state => {
return {
userInfo: state.userInfo,
};
};
const mapDispatchToProps = dispatch => {
return {
getUserInfo: () => {
console.log('clicked');
dispatch(UserInfoActions.userInfoRequest())
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(LoginScreen);
这是我的 redux 类
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
userInfoRequest: ['data'],
userInfoSuccess: ['payload'],
userInfoFailure: null
})
export const UserInfoTypes = Types
export default Creators
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
data: null,
fetching: null,
payload: null,
error: null
})
/* ------------- Selectors ------------- */
export const UserInfoSelectors = {
getData: state => state.data
}
/* ------------- Reducers ------------- */
// request the data from an api
export const request = (state, { data }) =>
state.merge({ fetching: true, data, payload: null })
// successful api lookup
export const success = (state, action) => {
const { payload } = action
return state.merge({ fetching: false, error: null, payload })
}
// Something went wrong somewhere.
export const failure = state =>
state.merge({ fetching: false, error: true, payload: null })
/* ------------- Hookup Reducers To Types ------------- */
export const reducer = createReducer(INITIAL_STATE, {
[Types.USER_INFO_REQUEST]: request,
[Types.USER_INFO_SUCCESS]: success,
[Types.USER_INFO_FAILURE]: failure
})
最后是我的 Saga 类
/* ***********************************************************
* A short word on how to use this automagically generated file.
* We're often asked in the ignite gitter channel how to connect
* to a to a third party api, so we thought we'd demonstrate - but
* you should know you can use sagas for other flow control too.
*
* Other points:
* - You'll need to add this saga to sagas/index.js
* - This template uses the api declared in sagas/index.js, so
* you'll need to define a constant in that file.
*************************************************************/
import { call, put } from 'redux-saga/effects'
import {UserInfoActions} from '../Redux/UserInfoRedux'
export function * getUserInfo (api, action) {
const { data } = action
// get current data from Store
// const currentData = yield select(UserInfoSelectors.getData)
// make the call to the api
const response = yield call(api.getUserInfo, data)
// success?
if (response.ok) {
// You might need to change the response here - do this with a 'transform',
// located in ../Transforms/. Otherwise, just pass the data back from the api.
yield put(UserInfoActions.userInfoSuccess(response.data))
} else {
yield put(UserInfoActions.userInfoFailure())
}
}
提前感谢您的帮助!
2个回答
这是我自己犯的愚蠢错误!导入时不知道 {} 的重要性。删除了导入中的 {},现在就可以正常工作了。
Angelhia de Fiesta
2020-02-21
在我的例子中,我将一个变量分配给一个钩子调用,但是我没有在该钩子调用中返回任何内容,所以我的变量不会收到任何值,因此值为
undefined
。
很简单:如果您收到
undefined
值,则意味着您的变量没有值,这不是您所期望的。
尝试调试您的代码以查看错误来自何处以及为什么它没有提供所需的输出。
记住:
undefined
表示没有值。
Gabriel Arghire
2021-03-24