React Native Redux createStore 错误:未定义不是对象(评估“action.type") [英] React Native Redux createStore error:undefined is not an object(evaluating 'action.type')
本文介绍了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
}
这篇关于React Native Redux createStore 错误:未定义不是对象(评估“action.type")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文