这里有错误 在“redux-thunk”中找不到导出“default”(作为“thunk”导入)(可能的导出:thunk、withExtraArgument)
这段代码是todosAction.js
import axios from "axios";
import {
GET_TODOS_FAILED,
GET_TODOS_REQUEST,
GET_TODOS_SUCCESS,
} from "../constants/todosConstant";
export const getAllTodos = () => async (dispatch) => {
dispatch({ type: GET_TODOS_REQUEST });
try {
const res = await axios.get("https://jsonplaceholder.typicode.com/todos");
dispatch({ type: GET_TODOS_SUCCESS, payload: res.data });
} catch (error) {
dispatch({ type: GET_TODOS_FAILED, payload: error.message });
}
};
这段代码是todosReducer.js
import {
GET_TODOS_FAILED,
GET_TODOS_REQUEST,
GET_TODOS_SUCCESS,
} from "../constants/todosConstant";
const initialState = {
isLoading: false,
todos: [],
error: null,
};
const todosReducer = (state = initialState, action) => {
switch (action.type) {
case GET_TODOS_REQUEST:
return {
...state,
isLoading: true,
};
case GET_TODOS_SUCCESS:
return {
isLoading: false,
todos: action.payload,
error: null,
};
case GET_TODOS_FAILED:
return {
isLoading: false,
todos: [],
error: action.payload,
};
default:
return state;
}
};
export default todosReducer;
这是我的 store.js 代码
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import todosReducer from "./services/reducers/todosReducer";
const store = createStore(todosReducer, applyMiddleware(thunk));
export default store;
之后我使用provide并将props存储到我的index.js文件中 当我检查错误时,这里显示的是我的依赖项 "react-redux": "^9.0.3", "redux": "^5.0.0", “redux-thunk”:“^3.1.0”,
我尝试从该 API 获取异步数据。我尝试了很多方法,但thunk错误没有解决
redux-thunk
自版本 3 起不再有默认导出。
相反,你需要做
import { thunk } from 'redux-thunk'
通常,您正在编写一种极其过时的 Redux 风格。 Modern Redux 不手动设置 thunk。它还不使用 switch..case 减速器、ACTION_TYPES、减速器中的手写不变性、
createStore
或 applyMiddleware
。
如果您刚刚设置此应用程序,您可能正在遵循 2019 年之前的教程。请遵循官方 Redux 教程。