2017-04-20 127 views
0

我使用终极版与ReactNative,我想创建一个存储与减速器阵营本地终极版createStore错误:未定义不是对象(计算“action.type”)

而且,我得到了以下错误,点到线在reducer.js在功能switchToTab( '开关(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()); 
} 
+0

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

回答

0

你不要在创建店铺call减速。 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 
} 
+0

THX,它适用于我! – Flame

+0

不错,REDX很棒玩得开心! – chris

相关问题