【问题标题】:Redux and React-Naitve: Cannot read property 'type' of undefinedRedux 和 React-Native:无法读取未定义的属性“类型”
【发布时间】:2019-03-10 16:34:41
【问题描述】:

我正在尝试在我的第一个 react-native 应用程序中设置鸭子和传奇。但是每当我尝试调用鸭子函数时,我都会收到一个红屏提示:Cannot read property 'type' of undefined

Redux 存储正在创建,但不知道为什么它没有在 switch 案例中采取行动。

以下代码来自我的鸭子/auth.js

import { fromJS, List } from 'immutable';

const action = name => `/auth/${name}`;

export const LOGIN_REQUEST = action('LOGIN_REQUEST');

export const loginRequest = () => ({ type: LOGIN_REQUEST, });

export const FETCH_LOGIN_REQUEST = action('FETCH_LOGIN_REQUEST');
export const fetchLoginRequest = login => ({ type: FETCH_LOGIN_REQUEST, login });

const initialState = {
  token: null,
}

const auth = (state = initialState, action) => {
  console.log('Hello Action: ', action.type);

  switch (action.type) {

    case FETCH_LOGIN_REQUEST:
      return state.set('token', action.login);

    default:
      console.log('Hello Auth Ducks Default')
      return state;
  }
};

export default auth;

【问题讨论】:

  • 你的 console.log(action.type) 输出什么?我假设未定义?
  • @JRK 这是行动。类型控制台:@@redux/INITk.h.c.n.b.f
  • @JRK,看来actionundefined,所以action.type抛出错误。你如何调用auth 函数?删除了你的 console.log,评论你的开关,并给我们返回 console.dir(action)
  • Reducer “auth” returned undefined when handling “@@redux/INIT4.f.t.o.4” action. To ignore an action, you must explicitly return the previous state. 卸下开关盒时出现以下错误。 console.dir(action)undefined
  • @AlexandreT 您可以通过运行代码来检查它。 https://github.com/rajdeepratan/naiudaan

标签: react-native redux redux-form redux-thunk redux-saga


【解决方案1】:

在welcome.js 中改变

import loginRequest from '../ducks/auth.js';

import {loginRequest} from '../ducks/auth.js';

loginRequest 是命名导出,而不是默认导出。

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 2019-06-03
    • 2020-10-10
    • 2019-02-15
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多