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

fix: prevent component re-rendering with the same account snapshot #4229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

weilliao05621
Copy link

In addressing performance issues with a specific React hook, I identified that components using this hook were re-rendering unnecessarily. This was caused by the hook destructuring an object, which created a new reference each time it executed.

// `trackedResult` will always has new reference when the hook executed
 const trackedResult = { ...result }

To optimize this, I use useMemo to wrap the returned object. This change prevents the creation of a new object reference on every execution, ensuring that the object is only recalculated when its dependencies change. As a result, the number of re-renders for components utilizing this hook has significantly decreased.

Screenshots

Have tried the updated version in the project's react playground.

Before

2024-08-27.8.35.24.mov

After

2024-08-27.8.34.09.mov

Copy link

changeset-bot bot commented Aug 27, 2024

⚠️ No Changeset found

Latest commit: 8ccf41d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Aug 27, 2024

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

Name Status Preview Comments Updated (UTC)
wagmi ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 27, 2024 1:01pm

@0x33dm
Copy link

0x33dm commented Sep 10, 2024

Nice catch! i hope this gets merged soon.

@tmm tmm force-pushed the main branch 2 times, most recently from 026feab to 9b7cc38 Compare September 11, 2024 21:57
@tmm
Copy link
Member

tmm commented Sep 16, 2024

Can you post the playground code that demonstrates this behavior or add a test that reproduces?

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.

3 participants