我需要使用redux thunk和axios从我的API中获取数据. 但我在控制台内遇到了这个错误.

ERROR IMAGE

在这种情况下,我有一个marketSlice,我想用API数据更新我的state.market.

marketSlice.js

import { createSlice } from '@reduxjs/toolkit'
import { createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios'

// Thunk action to fetch data from the server
export const fetchMarketData = (requestData) => createAsyncThunk(
  'market/fetchData', () => {
    const url = 'http://localhost:8000/api/market'
    const headers = {
      'Content-Type': 'application/json',
    };
    return axios
      .post(url, requestData, {headers})
      .then(response => response.data)
  }
);

export const marketSlice = createSlice({
  name: 'market',
  initialState: {
    market: null,
    loading: false,
    error: '',
  },
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchMarketData.pending, (state) => {
        state.loading = true;
        state.error = null;
    })
    builder.addCase(fetchMarketData.fulfilled, (state, action) => {
        state.loading = false;
        state.market = action.payload
    })
    builder.addCase(fetchMarketData.rejected, (state) => {
        state.loading = false;
        state.error = "Request Failed.";
    });
  },
});

store.js

import { configureStore } from '@reduxjs/toolkit'
import { marketSlice } from './marketSlice.js'

export const store = configureStore({
  reducer: {
    market: marketSlice.reducer
  },
  middleware: (getDefaultMiddleware)=> getDefaultMiddleware({
    serializableCheck: false,
  }),
})

App.jsx

import { useDispatch } from 'react-redux'
import { useEffect } from 'react'
import { fetchMarketData } from './redux/marketSlice.js'

export default function App() {
  const dispatch = useDispatch();

  useEffect(() => {
    const requestData = {
      exchange: "binance"
    }
    dispatch(fetchMarketData(requestData));

  }, [dispatch]);

  return (
    <>
        Something...
    </>
  )

}

推荐答案

您似乎已经将fetchMarketData编码为一个函数,而102是一个非同步thunk动作创建者函数,而只是返回的生成thunk动作本身.根据使用情况,我想说您打算将requestData作为传递给动作有效负载创建者的参数.

Inc或rect

exp或t const fetchMarketData = (requestData) => createAsyncThunk(
  'market/fetchData',
  () => {
    const url = 'http://localhost:8000/api/market'
    const headers = {
      'Content-Type': 'application/json',
    };
    return axios
      .post(url, requestData, { headers })
      .then(response => response.data);
  }
);

C或rect

exp或t const fetchMarketData = createAsyncThunk(
  'market/fetchData',
  (requestData, thunkApi) => { // <-- arg passed here
    const url = 'http://localhost:8000/api/market';
    const headers = {
      'Content-Type': 'application/json',
    };
    return axios
      .post(url, requestData, { headers })
      .then(response => response.data)
      .catch(err或 => thunkApi.rejectWithValue(err或));
  }
);

exp或t const fetchMarketData = createAsyncThunk(
  'market/fetchData',
  async (requestData, thunkApi) => { // <-- arg passed here
    const url = 'http://localhost:8000/api/market';
    const headers = {
      'Content-Type': 'application/json',
    };

    try {
      const { data } = axios.post(url, requestData, { headers });
      return data;
    } catch(err或) {
      return thunkApi.rejectWithValue(err或);
    }
  }
);

Reactjs相关问答推荐

useReducer和带有回调的useState之间是否有实际区别?

如何实现黑暗模式 map ?

在react.js的条形图中获取错误&unfined是不可迭代的

为什么我的React App.js只在页面刷新时呈现3次?

使用自动播放的视频组件呈现登录页面时出现JEST测试错误

Chart.js如何go 除边框

Chakra UI:如何在选中状态下设置复选框 colored颜色

BrowserRouter改变了URL但没有链接到页面

React 应用程序可以嵌入到 PowerPoint 中吗?

根据其中的值不同地react setState 更新状态

如何通过 id 从 useSprings 数组中删除元素

Material UI:使用 mui 手风琴时出现props 类型失败:isValidElement 不是函数

useState 在生命周期中的位置

如何从其他组件访问 useQuery refetch 方法?

如何在 MUI 数据网格中的 renderCell 组件之间传递状态

react-query、react-hook-form 和表单验证

如何实现两个 SVG 形状相互叠加的 XOR 操作?

找不到元素 - 这是参考问题吗?

无法有条件地更新useEffect中的setState

为什么 state redux-toolkit 是代理?