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

WIP: perf: avoid re-renders caused by useEffect #4164

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

Conversation

WofWca
Copy link
Collaborator

@WofWca WofWca commented Oct 2, 2024

Having read https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes, I think we might be overusing useEffect to detect prop/state changes.
I'm not a huge React expert, but I would like to ask if these changes make sense.

useEffect(() => {
const queryStrHasChanged = useHasChanged2(queryStr)
// TODO why don't we just call this unconditionally?????
// `Object.is()` will take care of infinite loops.
Copy link
Member

Choose a reason for hiding this comment

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

I assume Object.is() is part of react's implementation of setState?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's right https://react.dev/reference/react/useState#setstate

If the new value you provide is identical to the current state, as determined by an Object.is comparison, React will skip re-rendering the component and its children. This is an optimization. Although in some cases React may still need to call your component before skipping the children, it shouldn’t affect your code.

Copy link
Member

Choose a reason for hiding this comment

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

not completely clear from the comment alone, better mention that this is done by setState

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, this is just a rough draft

@WofWca WofWca added the performance Related to (improving) performance label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Related to (improving) performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants