简体   繁体   English

错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 在 React Native 中

[英]Error: Actions must be plain objects. Use custom middleware for async actions. in React Native

I'm getting this error every time I try to dispatch my action:每次尝试分派我的动作时都会收到此错误:

Error: Actions must be plain objects.错误:操作必须是普通对象。 Use custom middleware for async actions.使用自定义中间件进行异步操作。

I've installed redux-thunk and without async actions, it's working.我已经安装了redux-thunk并且没有异步操作,它正在工作。

Store config:店铺配置:

import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { composeWithDevTools } from 'redux-devtools-extension';

import reducers from '../reducers/index';

const logger = createLogger();

export default createStore(reducers, composeWithDevTools(applyMiddleware(thunk)));

UI:用户界面:

...
import { connect } from 'react-redux';
import { getCities } from '../../actions/cities';
...
componentDidMount = async () => {
    try {
      const cities = await this.props.getCities();
      console.log(cities);
    } catch (error) {
      console.log(`errorhome: ${error}`);
    }
    SplashScreen.hide();
  }
...

const mapDispatchToProps = dispatch => ({
  changeNetworkStatus: () => dispatch(changeNetworkStatus),
  getCities: () => dispatch(getCities),
});

export default connect(mapStateToProps, mapDispatchToProps)(Home);

Action:行动:

import database from '../config/utils';

export const GET_CITIES_START = 'GET_CITIES_START';
export const GET_CITIES_FINISHED = 'GET_CITIES_FINISHED';
export const GET_CITIES_ERROR = 'GET_CITIES_ERROR';

const getCititesStart = () => ({ type: GET_CITIES_START });
const getCititesFinished = cities => ({ type: GET_CITIES_FINISHED, cities });
const getCititesError = error => ({ type: GET_CITIES_ERROR, error });

export const getCitites = () => async (dispatch) => {
  dispatch(getCititesStart());
  try {
    const cities = [];
    const snap = await database.ref('cities').once('value');
    console.log('snap: ', snap);
    snap.forEach((element) => {
      const city = {
        city: element.key,
        coordinate: element.val().coordinate,
      };
      cities.push(city);
    });
    dispatch(getCititesFinished(cities));
  } catch (error) {
    dispatch(getCititesError(error));
  }
};

EDIT: If I add logger to middlewares too, the error message is this:编辑:如果我也将logger添加到中间件,错误消息是这样的:

TypeError: Cannot read property 'type' of undefined类型错误:无法读取未定义的属性“类型”

Thanks for your help!谢谢你的帮助!

Actions are functions that return a object with action's data, that data is a object with a type property.动作是返回带有动作数据的对象的函数,该数据是具有type属性的对象。

You're dispatching action like this:你正在调度这样的动作:

dispatch(getCities)

You should dispatch action like this:你应该分派这样的动作:

dispatch(getCities())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error 收到错误消息“动作必须是普通对象。 使用自定义中间件进行异步操作。”使用AXIOS时在React Native中 - Getting an error “Actions must be plain objects. Use custom middleware for async actions.” in React Native when using AXIOS “行动必须是普通的对象。 使用自定义中间件进行异步操作。“with react / redux - “Actions must be plain objects. Use custom middleware for async actions.” with react/redux 动作必须是普通对象。 使用自定义中间件进行异步操作。 React,Redux,Thunk - Actions must be plain objects. Use custom middleware for async actions. React, Redux, Thunk 动作必须是普通对象。 使用自定义中间件进行异步操作。 react-redux - Actions must be plain objects. Use custom middleware for async actions. react-redux Redux 给出错误“错误:操作必须是普通对象。 使用自定义中间件进行异步操作。” - Redux gives error “Error: Actions must be plain objects. Use custom middleware for async actions.” 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 - Error: Actions must be plain objects. Use custom middleware for async actions. 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 待办事项应用 - Error: Actions must be plain objects. Use custom middleware for async actions. ToDo app 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 如何解决? - Error: Actions must be plain objects. Use custom middleware for async actions. How to fix it? 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM