Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(jotai/query): same query client initialisation #1294

Merged
merged 4 commits into from
Jul 21, 2022

Conversation

austinwoon
Copy link
Contributor

@austinwoon austinwoon commented Jul 20, 2022

I had a project with existing integrations with react-query before importing jotai to the project.

Upon using atomWithQuery without this initialisation step, I realised that my existing functions which called queryClient.invalidate(...) did not result in atomWithQuery's query invalidating. As a result, data was not refetched and the atom had stale data.

After browsing the discussions, I found the answer here #500 (reply in thread). The cause was because our functions were referencing different instances of queryClient

Seeing that this was a discussion thread, and that others might port in jotai into existing projects which already have react-query integrated, I thought it might be helpful to add this piece of documentation into the official query integration documentation.

Please let me know if the example and documentation is clear! Thank you.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@vercel
Copy link

vercel bot commented Jul 20, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
jotai ✅ Ready (Inspect) Visit Preview Jul 21, 2022 at 1:44AM (UTC)

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 20, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 91f042b:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
Next.js Configuration
Next.js with custom Babel config Configuration
React with custom Babel config Configuration

@austinwoon austinwoon changed the title docs: same query client initialisation docs(query-integrations): same query client initialisation Jul 20, 2022
@austinwoon austinwoon changed the title docs(query-integrations): same query client initialisation docs(jotai/query): same query client initialisation Jul 20, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great addition. Thanks for working on it.


Although they reference methods same query keys (`'todos'`), should `useTodoMutation` be called and invalidate the query without the `Provider` initialisation step, `todosAtom` will not be prompted to refetch and will continue showing stale data.

```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's helpful to have import statements in the code snippet.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

export const useTodoMutation = () => {
const queryClient = useQueryClient()

return useMutation(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMutation is unavoidable at this point.
But, from the jotai/query perspective (which is not tied to React),
we would like to have atomWithMutation. (We have it in jotai/urql.)
Would you be interested in contributing in it?? Issue 👉 #309

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, would love to attempt and contribute to it.

Will try to implement a wrapper in my existing project and make a new PR when its ready!

Thanks for the quick review, great library! 🥳

@dai-shi dai-shi merged commit 09fad0a into pmndrs:main Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants