ios - react native : Yet another "Undefined is not an object (evaluating action. 类型)

标签 ios react-native react-redux create-react-native-app

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

“未定义不是一个对象(正在评估 action.type)

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

this one ,这与在异步调度程序之前调用的 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

关于ios - react native : Yet another "Undefined is not an object (evaluating action. 类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716099/

相关文章:

reactjs - 可以从 Redux reducer 中发出 REST API 请求吗?

ios - 两个 UIButtons 共享代码

ios - NSPredicate 包含搜索包含数字和字母的字符串

react-native - 使用未锚定坐标的图像对 native map 标记使用react

javascript - 无法读取未定义的属性 'routeName' - react 抽屉导航

reactjs - 当我使用 react 路由器单击后退按钮时,页面未呈现

ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音

ios - 尝试 Apple 的 Sample AVCAM 发现错误

reactjs - Typescript/React 开始获取大量 "variableName is undefined"

javascript - Next.JS Redux 调度在 getStaticProps() 中不起作用