【发布时间】:2021-09-13 12:15:16
【问题描述】:
API
import axios from "axios"
const url = "http://localhost:5000/posts"
export const fetchPosts = () => async () => await axios.get(url)
export const createPost = async (post) => await axios.post(url, post)
动作
export const fetchPosts = async (dispatch) => {
try {
const {data} = await api.fetchPosts()
dispatch({
type: types.FETCH_POSTS,
payload: data
})
} catch (err) {
console.error(err)
}
}
商店
import { createStore, applyMiddleware } from 'redux'
import rootReducer from './index'
import thunk from 'redux-thunk'
export default function configureStore() {
return createStore(rootReducer, applyMiddleware(thunk))
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { Provider } from 'react-redux'
import configureStore from './Redux/store/configureStore'
const store = configureStore();
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
我想通过 axios 向我的帖子发出 get 请求。 post请求没有问题,但我无法获得。
useEffect(() => {
dispatch(fetchPosts())
}, [dispatch])
当我使用这个方法时,它会抛出这个错误:
错误:动作必须是普通对象。相反,实际的类型是:'Promise'。您可能需要将中间件添加到您的商店设置中以处理调度其他值,例如“redux-thunk”来处理调度函数。
没有任何语法或导入/导出错误。
为什么即使我插入了 redux thunk 来存储它也会抛出这个中间件错误?
【问题讨论】:
标签: reactjs redux redux-thunk