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

perf: avoid extra rerender #2558

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

perf: avoid extra rerender #2558

wants to merge 4 commits into from

Conversation

hjiog
Copy link

@hjiog hjiog commented May 17, 2024

Summary

useAtomValue called rerender inside useEffect, which led to an additional render, and this PR optimized that performance issue add a optimization test.

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented May 17, 2024

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

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 9:52am

Copy link

codesandbox-ci bot commented May 17, 2024

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.

@dai-shi
Copy link
Member

dai-shi commented May 17, 2024

We should probably add another test to ensure that rerender() in useEffect is required, before tackling this.

If anyone is interested, contributions are welcome. It requires decent knowledge about React rendering.

Another point is that this optimization should be handled with React binding. There's nothing wrong in store implementation.

@dai-shi dai-shi marked this pull request as draft May 17, 2024 06:02
Copy link

LiveCodes Preview in LiveCodes

Latest commit: 515f416
Last updated: May 17, 2024 6:00am (UTC)

Playground Link
React demo https://livecodes.io?x=id/7EV87YTT2

See documentations for usage instructions.

@hjiog hjiog marked this pull request as ready for review May 28, 2024 09:50
@hjiog hjiog changed the title add a optimization test to avoid extra rerender perf: avoid extra rerender May 28, 2024
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.

We should probably add another test to ensure that rerender() in useEffect is required, before tackling this.

Please create a test for rerender() first before tackling this. Otherwise, you don't know why we need it. I think it's reasonable to create a separate PR.

Another point is that this optimization should be handled with React binding. There's nothing wrong in store implementation.

Please do not modify store implementation.

@hjiog
Copy link
Author

hjiog commented May 28, 2024

If I have uderstood correctly, rerender() is to ensure that all the components have executed the subscription logic, so that there will be no problem when the onMounted method is called. But I don't know how to create a test for rerender(). Yea, this optimization can be handled with React binding, but what if we do it better? This PR can be used as a reference idea. If someone is interested, welcome to improve it.

@hjiog hjiog marked this pull request as draft May 28, 2024 12:34
@dai-shi
Copy link
Member

dai-shi commented May 28, 2024

Yeah, let's leave it for now. Here's some comments for the future.

rerender() exists because useEffect is async.
I think useLayoutEffect instead might be a fix (but I haven't confirmed),
but it will probably be a major bump with React 19 requirement (or maybe 18).

If we were to optimize it in Jotai v2, it must be within the hook.
It should be technically possible with something like use-sync-external-store shim.

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