![](/img/trans.png)
[英]React Native Redux : “Undefined is not an object (evaluating 'action.type')”
[英]React Native Redux createStore error:undefined is not an object(evaluating 'action.type')
我将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());
}
创建商店时,您无需致电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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.