React Native Redux createStore 错误:未定义不是对象(评估“action.type")

问题描述:

我使用 Redux 和 ReactNative,我想用 reducer 创建一个 store

I'm use Redux with ReactNative,I'd like to create a store with reducer

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

And,I got error below, point to line 'switch (action.type)' in function switchToTab() in reducer.js

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

这是我的 actions.js

Here is my actions.js

export const SWITCH_TAB = 'switchTab'

export function switchTab(index) {

return {
    type: SWITCH_TAB,
    index: index
}

}

这是我的 reducer.js

Here is my 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:

Here is 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());
}

你在创建 store 时没有调用 reducer.createStore 接受一个 reducer 函数作为它的第一个参数.

You dont call the reducer when you create the store. createStore accepts a reducer function as its first argument.

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
}