admin管理员组文章数量:1293512
I'm working with React Query and try to get data (users) from a JSON file inline.
I have installed it with npm React-query
& React-query-devtools and have only 2 files.
App.js
:
import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactQueryDevtools } from 'react-query-devtools';
import Users from './Users';
function App() {
return (
<div className="App">
<QueryClientProvider client={QueryClient}>
<Users />
<ReactQueryDevtools initialIsOpen />
</QueryClientProvider>
</div>
);
}
export default App;
Users.js
:
import React from 'react'
import { useQuery } from 'react-query';
const Users = () => {
const { isLoading, isError, data, error } = useQuery('test', () => (
fetch('').then((res) => res.json())
));
return (
<div>
{isLoading ? <h2>Chargement...</h2> : null}
{isError ? <h2>Une erreur est survenue : {error.message}</h2> : null}
<ul>
{data.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
)
}
export default Users
I can't solve this error:
TypeError: queryClient.defaultQueryObserverOptions is not a function
I'm working with React Query and try to get data (users) from a JSON file inline.
I have installed it with npm React-query
& React-query-devtools and have only 2 files.
App.js
:
import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactQueryDevtools } from 'react-query-devtools';
import Users from './Users';
function App() {
return (
<div className="App">
<QueryClientProvider client={QueryClient}>
<Users />
<ReactQueryDevtools initialIsOpen />
</QueryClientProvider>
</div>
);
}
export default App;
Users.js
:
import React from 'react'
import { useQuery } from 'react-query';
const Users = () => {
const { isLoading, isError, data, error } = useQuery('test', () => (
fetch('https://jsonplaceholder.typicode./users').then((res) => res.json())
));
return (
<div>
{isLoading ? <h2>Chargement...</h2> : null}
{isError ? <h2>Une erreur est survenue : {error.message}</h2> : null}
<ul>
{data.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
)
}
export default Users
I can't solve this error:
TypeError: queryClient.defaultQueryObserverOptions is not a function
Share
Improve this question
edited Jan 8, 2021 at 14:25
double-beep
5,51919 gold badges40 silver badges49 bronze badges
asked Jan 8, 2021 at 9:19
Jonathan ViaultJonathan Viault
331 gold badge1 silver badge3 bronze badges
0
1 Answer
Reset to default 7You should use an instance of QueryClient
for the client
prop, not the QueryClient
directly.
As per the docs
import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactQueryDevtools } from 'react-query-devtools';
import Users from './Users';
// Create a client
const queryClient = new QueryClient() // Instance of QueryClient
function App() {
return (
<div className="App">
<QueryClientProvider client={queryClient}> // <-- here
<Users />
<ReactQueryDevtools initialIsOpen />
</QueryClientProvider>
</div>
);
}
export default App;
本文标签: javascriptTypeError queryClientdefaultQueryObserverOptions is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: queryClient.defaultQueryObserverOptions is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741578653a2386439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论