-
Was reading some resources related to the repository around using a param to make a request but would like to know if there is any recipe for doing this with the react query plugin? Reading references so far: the idea around what i want to achieve is to trigger a query doing the following import React, { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { Provider, atom, useAtom } from 'jotai';
import { useAtomValue, useUpdateAtom } from 'jotai/utils';
import { atomWithQuery } from 'jotai/query';
const setting = atomWithQuery((get) => ({
// id should be an input somehow
...buildQueryConfig({ id }),
}));
const fetchAtom = atom(
// unclear what should go here
);
export const Component = () => {
const { id } = useParams();
const [data, fetch] = useAtom(fetchAtom);
useEffect(() => {
fetch(id);
}, []);
return (
<>
something to display
</>
);
}; Is this the right track or is there a better way? Also advice on the unclear parts would be nice Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 44 replies
-
Hey, thanks for asking. const idAtom = atom(null)
const dataAtom = atomWithQuery((get) => {
const id = get(idAtom)
if (id === null) {
return ... // OOPS: there's no way to stop initialization...
}
return buildQueryConfig({ id })
})
export const Component = ({ id }) => {
const setId = useUpdateAtom(idAtom)
useEffect(() => {
setId(id)
}, [setId, id])
return <>something to display</>
} So, there's a missing feature in @Aslemammad any other idea? |
Beta Was this translation helpful? Give feedback.
-
As a side note, I started looking into jotai as a way to manage a client heavy state component for an app that already uses react-query. What interested me was the react-query integration and also the recipe for managing large objects. it is pretty neat With that i had to ensure that only one query client was being used so this is what i did import React, { useEffect } from 'react';
import { QueryClientProvider, QueryClient } from 'react-query';
import { queryClientAtom } from 'jotai/query';
import { useUpdateAtom } from 'jotai/utils';
const queryClient = new QueryClient();
export const JotaiQueryProvider = ({ children }) => {
const setQueryClient = useUpdateAtom(queryClientAtom);
useEffect(() => {
setQueryClient(queryClient);
}, [setQueryClient]);
return (
<>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</>
);
}; May be helpful for someone else i guess |
Beta Was this translation helpful? Give feedback.
-
This might be helpful! #535 |
Beta Was this translation helpful? Give feedback.
As a side note, I started looking into jotai as a way to manage a client heavy state component for an app that already uses react-query.
What interested me was the react-query integration and also the recipe for managing large objects. it is pretty neat
With that i had to ensure that only one query client was being used so this is what i did