-
Notifications
You must be signed in to change notification settings - Fork 143
[CLNP-6022] Memoize useGroupChannel actions to prevent unnecessary re-rendering #1288
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wrapped all handlers with useCallback one by one. No logic changes. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import isEqual from 'lodash/isEqual'; | ||
|
|
||
| // Referrence: https://github.com/pmndrs/zustand | ||
| export type Store<T> = { | ||
| getState: () => T; | ||
|
|
@@ -7,7 +9,16 @@ export type Store<T> = { | |
|
|
||
| export function hasStateChanged<T>(prevState: T, updates: Partial<T>): boolean { | ||
| return Object.entries(updates).some(([key, value]) => { | ||
| return prevState[key as keyof T] !== value; | ||
| if (typeof prevState[key as keyof T] === 'function' && typeof value === 'function') { | ||
| /** | ||
| * Function is not considered as state change. Why? | ||
| * Because function is not a value, it's a reference. | ||
| * If we consider non-memoized function as state change, | ||
| * it will always be true and cause unnecessary re-renders. | ||
| */ | ||
| return false; | ||
| } | ||
| return !isEqual(prevState[key as keyof T], value); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the way of comparing prev <-> next state with |
||
| }); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.