【问题标题】:React Native Redux createStore error:undefined is not an object(evaluating 'action.type')React Native Redux createStore 错误:未定义不是对象(正在评估“action.type”)
【发布时间】:2017-09-16 12:09:31
【问题描述】:

我正在使用 Redux 和 ReactNative,我想用 reducer 创建一个商店

并且,我在下面遇到错误,指向 reducer.js 中函数 switchToTab() 中的“switch (action.type)”行

undefined is not an object(evaluating 'action.type')

这是我的actions.js

export const SWITCH_TAB = 'switchTab'

export function switchTab(index) {

return {
    type: SWITCH_TAB,
    index: index
}

}

这是我的 reducer.js

import { SWITCH_TAB } from './actions.js'

export function switchToTab(state = {}, action) {

switch (action.type) {//error point to this line

    case SWITCH_TAB:
        return Object.assign({}, ...state, {
            index: action.index
        });
    break;

    default:
        return state;
}

}

这里是createStore:

import { createStore } from 'redux';
import { switchToTab } from './reducer.js'

export default class MainPage extends Component {
    constructor(props) {
    super(props);
    this.state = {
        index:0
    };

    let store = createStore(switchToTab());
}

【问题讨论】:

  • 分派动作的代码在哪里?看起来它正在调度一个空变量而不是一个动作

标签: javascript react-native redux react-redux


【解决方案1】:

创建商店时不要调用减速器。 createStore 接受一个 reducer 函数作为它的第一个参数。

import { createStore } from 'redux';
import { switchToTab } from './reducer.js'

export default class MainPage extends Component {
    constructor(props) {
    super(props);
    this.state = {
        index:0
    };

    let store = createStore(switchToTab); // dont call this here, just pass it
}

【讨论】:

  • 不错,redux 很好玩!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多