gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:56:32 25 4
gpt4 key购买 nike

我正在开发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com