【问题标题】:React Native: Yet another "Undefined is not an object (evaluating action.type)React Native:又一个“未定义不是对象(评估 action.type)
【发布时间】:2018-08-06 21:49:10
【问题描述】:

我正在开发一个 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;
}

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

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

【问题讨论】:

  • 如果你去掉你的开关盒,是不是去掉了错误?

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


【解决方案1】:

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

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

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

如 Reducers 中所述,它必须具有 (previousState, action) => newState 的签名,称为 reducer 函数,并且必须是纯的和可预测的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多