React Native Redux createStore错误:undefined不是对象(评估'action.type')

时间:2017-04-20 03:26:25

标签: javascript react-native redux react-redux

我使用Redux和ReactNative,我想创建一个带减速器的商店

而且,我收到错误,指向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());
}

1 个答案:

答案 0 :(得分:0)

创建商店时不要调用reducer。 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
}