我在redux中遇到了这个问题,我运行了一个api服务,收到的响应很好。然而,在我添加另一个减速器路径reducer:{ [cryptoApi.reducerPath]:cryptoApi.reducer, },
之后我对所有其他服务的响应变得不明确。即使从那时起我只返回一个reducer路径,所有请求都会返回未定义的路径。在领事的网络部分找到的回应照片
领事中发现的同一api的第二次响应照片
有人知道发生了什么事吗?
Api服务设置
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
const cryptoHeaders = {
'X-RapidAPI-Host': 'coinranking1.p.rapidapi.com',
'X-RapidAPI-Key': '9f2533afaemsh1f8a925387397f8p1fea4ejsn485665c91539'
};
const baseUrl = "https://coinranking1.p.rapidapi.com";
const createRequest= (url) => ({
url,
headers:cryptoHeaders
})
export const cryptoApi = createApi({
reducerPath: "crytopApi",
baseQuery: fetchBaseQuery({baseUrl}),
endpoints: (builder) => ({
getCryptoData: builder.query({
query: () => createRequest(`/coins`),
}),
}),
});
export const {useGetCryptoDataQuery} = cryptoApi
我在上称之为
import { Routes, Route, Link } from "react-router-dom";
import { useGetCryptoDataQuery } from "./services/cryptoApi";
import { useGetnewsDataQuery } from "./services/newsApi";
import "./app.css";
const App = () => {
const { cryptoData, cryptoError, isLoadingCrypto } = useGetCryptoDataQuery();
console.log(cryptoData)
return <div>{JSON.stringify(cryptoData)}</div>;
};
export default App;
存储设置
import {configureStore} from '@reduxjs/toolkit'
import { cryptoApi } from '../services/cryptoApi'
export const store = configureStore({
reducer:{
[cryptoApi.reducerPath]:cryptoApi.reducer,
},
})
我确实打电话给供应商并通过商店
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import { store } from "./store/store.js";
import { Provider } from "react-redux";
import App from "./App.jsx";
import "./index.css";
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<Provider store={store}>
<App />
</Provider>
</BrowserRouter>
</React.StrictMode>,
document.getElementById("root")
);
原因是添加了另一个具有相同函数名的reducer。你的商店是这样的:
export const store = configureStore({
reducer: {
[cryptoApi.reducerPath]:cryptoApi.reducer,
[cryptoApi.reducerPath]:cryptoApi.reducer
}
})
相反,它应该是这样的:
export const store = configureStore({
reducer: {
[cryptoApi.reducerPath]:cryptoApi.reducer,
[cryptoNewsApi.reducerPath]:cryptoNewsApi.reducer
}
})