[英]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.