我是React Native的新手,我正在使用Redux-Saga。不幸的是,有一个我无法解决的错误。
我在分派动作时收到此错误:
TypeError:未定义不是对象(正在评估'_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 })
最后是我的传奇课
/* *********************************************************** * 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()) } }
谢谢您的帮助!
我是React Native的新手,我正在使用Redux-Saga。不幸的是,有一个我无法解决的错误。当我调度一个动作时,我得到这个错误:TypeError:undefined不是一个对象(...
这是我自己的愚蠢错误!导入时不知道{}的重要性。在我的一项导入中删除了{},并且现在可以正常使用。