React Native:又一个“未定义不是对象(评估action.type)

React Native: Yet another "Undefined is not an object (evaluating action.type)

我正在开发 CRNA 应用程序,但是商店连接不工作,我在创建商店时收到上述错误。

"未定义不是一个对象(计算action.type)

搜索类似的问题,我找到了 ,这是一个在传递给 createStore 函数时被调用的 reducer,这不是我的情况。

,这与在异步调度程序之前调用的 AnalyticsTracker 有关,也不是我的情况。

这是要重现的最少代码。

App.js

import React from 'react';
import {
  View,
  Text
} from 'react-native'; 
import { Provider } from 'react-redux';
import store from './store';

class App extends React.Component {

  render() {
    return (
      <Provider store={store}>
         <View>
            <Text>Hello</Text>
         </View>
      </Provider>
    );
  }
}

store.js

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducer';
// Here the error happens
export default createStore(reducer, applyMiddleware(thunk));

reducer.js

import actionTypes from './action_types';
const initialState = {
}

export default (action, state=initialState) => {
    // This is the top line on stacktrace
    switch (action.type) {
        case actionTypes.MY_ACTION:
            return state;
    }
    return state;
}

我已经尝试对我的代码进行一些更改,即:删除中间件。

知道为什么会这样吗?我错过了什么吗?

我注意到您的 createStore 调用是错误的,因为增强器作为第三个参数传递。将其更改为:

const store = createStore(persistedReducer, undefined, applyMiddleware(thunk));

另外你的reducer的结构是错误的。 reducer 中的第一个参数应该是 initialState,然后是作为第二个参数的 action - 这就是为什么你得到 undefined is not an object!

As described in Reducers, it has to have a signature of (previousState, action) => newState, is known as a reducer function, and must be pure and predictable.

发件人:https://redux.js.org/recipes/structuringreducers