我一直在尝试将 redux 实现到我的 React 应用程序中并使用 this guide进行ajax调用。
Example Actions Example Container
我的代码几乎和他的一样,只是我得到了错误:
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions
我查看了他的所有代码,他没有应用中间件或做任何异常的事情
我的异步操作
import axios from 'axios';
const GET_POSTS = 'GET_POSTS';
const getPosts = () => {
const request = axios.get('/url');
return { type: GET_POSTS, payload: request };
};
我的容器使用情况
import { getPosts } from './actions';
//other stuff
const mapDispatchToProps = dispatch => {
return {
getPosts: () => {
dispatch(getPosts()).then(response => {
console.log(response);
});
}
};
};
//connect functions to component
我猜他的例子行不通?我到处寻找解决方案,我知道 dispatch 会返回函数返回的任何内容(在这种情况下这不是 promise )。我知道如果我想让函数返回一个 promise ,我将不得不使用中间件。我只是对我目前是否做错了什么(那是什么)感到困惑,或者他的榜样是否被打破了?感谢您的帮助
最佳答案
在指南中,您正在使用的中间件已应用。请检查“第 8 步”:
...
import promise from 'redux-promise';
...
//Configure middleware w/ redux-promise for AJAX requests
const createStoreWithMiddleware = applyMiddleware(
promise
)(createStore);
关于javascript - react redux 异步操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130177/