-
Notifications
You must be signed in to change notification settings - Fork 1.3k
refactor: use getter functions to avoid unnecessary re-renders #890
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
Merged
shuding
merged 5 commits into
vercel:master
from
koba04:use-object-assign-to-avoid-rerender
Jan 12, 2021
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2fa533f
refactor: use Object.assign to avoid unnecessary re-renders
koba04 5cf5683
refactor: use getter functions to avoid unnecessary re-renders
koba04 116fced
refactor: use Object.assign to avoid unnecessary re-renders
koba04 ac512da
Revert "refactor: use Object.assign to avoid unnecessary re-renders"
koba04 352e03f
refactor: define properties with enumerable: true
koba04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.assign seems to have the same effect with object spread?
I think you can just assign to swr itself? the 1st arg won't be traversed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback!
You are right, to avoid triggering the getter function, I have to mutate
a
directly.But, that would break the current behavior, so I'll investigate a way to avoid triggering re-renders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A way I came up with is to define getter functions to evaluate properties lazily.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain a bit why
Object.assign(swr, { ... })
won't work? It does trigger the getter when accessinga.x
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shuding
The reason is that this mutates
swr
directly, and it breaks where theswr.mutate
variable is used.swr/src/use-swr-infinite.ts
Line 198 in 3121592
It leads to failing many tests. I can fix it like the following, but the
useSWRInfinite › should keep mutate referential equal
test is still failing. Should I investigate the way to pass the test rather than using getter functions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The failed test is here.
swr/test/use-swr-infinite.test.tsx
Line 441 in 3121592
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point! I think we need to find an elegant way to fix it then... let me see
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shuding I agree with you, that's not an ideal solution. I'll try to find it. Thank you for your feedback!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the same logic like
use-swr
then:The reason of not using the latest
get
syntax is that we have to specifyenumerable: true
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated specifing
enumerable: true
!I also came up with the way to use
useMemo
like 116fced, but React says "You may rely on useMemo as a performance optimization, not as a semantic guarantee. ", so I've reverted the change.