反应 - Query返回未定义
我正在使用React 18.2与Redux Toolkit和React-Query一起使用。
我将代码设置为以下内容,但是我在React-Query 中获得了 不确定的数据返回。
但是,在检查Chrome浏览器中的“网络”选项卡时,我确实会得到响应。
第一次实施反应问题。我在做什么错?
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import persistStore from 'redux-persist/es/persistStore';
import { QueryClient, QueryClientProvider } from 'react-query';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from './redux/store';
import './i18n';
const persistor = persistStore(store);
const queryClient = new QueryClient();
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<PersistGate persistor={persistor}>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</PersistGate>
</Provider>,
);
reportWebVitals();
app.js
import { useQuery} from 'react-query';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
function App() {
const { isLoading, error, data } = useQuery('repoData', async () => {
const { data } = await axios.get('https://api.github.com/repos/tannerlinsley/react-query');
return data;
});
if (isLoading) {
return <>Loading</>
}
if (error) {
return <>Error</>
}
console.log(data);
return (
<pre>{JSON.stringify(data)}</pre>
);
}
export default App;
I am using react 18.2 with redux toolkit and react-query.
I set up my code as follow, but I got data return in react-query as undefined.
However, I do get the response when inspecting the network tab in chrome browser.
First time implementing react-query. What am I doing wrong?
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import persistStore from 'redux-persist/es/persistStore';
import { QueryClient, QueryClientProvider } from 'react-query';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from './redux/store';
import './i18n';
const persistor = persistStore(store);
const queryClient = new QueryClient();
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<PersistGate persistor={persistor}>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</PersistGate>
</Provider>,
);
reportWebVitals();
App.js
import { useQuery} from 'react-query';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
function App() {
const { isLoading, error, data } = useQuery('repoData', async () => {
const { data } = await axios.get('https://api.github.com/repos/tannerlinsley/react-query');
return data;
});
if (isLoading) {
return <>Loading</>
}
if (error) {
return <>Error</>
}
console.log(data);
return (
<pre>{JSON.stringify(data)}</pre>
);
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是此代码
使用这种方式
instead of this code
use this way
修复:
您需要从查询中提取的
data
变量重命名:请注意,我已将您的函数提取到
fetchdata
中的查询中,但它保持不变。您可以按照自己的意愿命名您的变量。这是与变量命名有关的范围问题。在代码中,您正在破坏使用相同的变量名称数据的Axios响应和React查询响应。这似乎使变量发生冲突。
提示:
我想建议您使用
@tanstack/react-Query
。请注意,如果您执行查询键,则作为数组传递,因此您需要像这样修改查询:我还建议使用React Query Dev工具。这将帮助您进一步调试问题。在您的
index.js
中添加此行:然后将此行放入您的QueryClientProvider组件中:
看起来像这样:
Fix:
You need to rename the
data
variable that you extract from your query:Note that I have extracted your function passed to the query in a
fetchData
but it remains unchanged. You can name your variable as you wish.It's a scope issue related to the variable naming. In your code, you are destructuring both the Axios response and the React Query response with the same variable name, data. This seems to make the variables clash.
Tip:
I'd like to suggest you use
@tanstack/react-query
as it is recommended. Mind that if you do the query key is then passed as an array so you will need to modify your query like so:I also recommend using the react query dev tools. This will help you further debug problems. In your
index.js
add this line:And then place this line in your QueryClientProvider component:
It will look like this: