šŸ“• Build AI Agents using LangGraph.js is now out!

Fixing the Uncaught Error: No QueryClient set, use QueryClientProvider to set one

While working on an example for pagination with useInfiniteQuery() I have stumbled on the following error:

Uncaught Error: No QueryClient set, use QueryClientProvider to set one

My initial code was looking like this:

/* ā›”ļø getting the following error on this code:
No QueryClient set, use QueryClientProvider to set one */ 
const App = () => {
    const {isLoading, error, data} = useQuery({
        queryKey: ['usersData'],
        queryFn: () =>
        fetch('https://randomuser.me/api').then(res => res.json()),
    });

    if (isLoading) return 'ā³ loading...';
    if (error) return 'An error has occurred!';

    return (<div>
        {data.results.map(user => (
            <div key={user.id.value}>
                šŸ‘Øā€šŸ’» User: {user.name.first} {user.name.last}
            </div>
        ))}
    </div>);
}

After some research, I've discovered that in order to get the ReactQuery to work I need to do the following:

Below is how the code should have looked like:

// ā¶ make a new queryClient 
const queryClient = new QueryClient();

// ā· extract the useQuery() part in its own component
const Results = () => {
    const {isLoading, error, data} = useQuery({
        queryKey: ['usersData'],
        queryFn: () =>
        fetch('https://randomuser.me/api').then(res => res.json()),
    });

    if (isLoading) return 'ā³ loading...';
    if (error) return 'An error has occurred!';

    return (<div>
        {data.results.map(user => (
            <div key={user.id.value}>
                šŸ‘Øā€šŸ’» User: {user.name.first} {user.name.last}
            </div>
        ))}
    </div>);
}

// āø wrap the component in a QueryClientProvider
const App = () => (
    <QueryClientProvider client={queryClient}>
        <Results />
    </QueryClientProvider>
)

You can see the working example on GitHub pages and the full code in the repo.

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js


Leave a Reply

Your email address will not be published. Required fields are marked *