-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
useRef instead of useMemo #6
Conversation
I am happy that this passed the unit test that checks for the problem that Can you provide a test that would fail with the current |
I can't provide test speaking for useRef. Perhaps someone from React team can 😃 Here is quote from docs, taken from https://reactjs.org/docs/hooks-reference.html#usememo:
|
That quote doesn't seem to be against the pattern used here. It's unfortunate that it's not a semantic guarantee, but performance takes a higher priority, as the purpose of passing a static reference to The current implementation does work without I'll leave this open to discussion, but I'm not currently convinced this change is in the best interest of Components relying on this feature. |
In my understanding useMemo was used here to maintain the same forceUpdate
reference during component lifecycle.
This is exactly the use case useRef is designed for - to maintain a single
value during component lifecycle, that's why I changed that.
If in the future React will decide to clear useMemo cache based on some
conditions, current implementation may break something as forceUpdate
reference will change occasionally.
…On Thu, Apr 4, 2019, 10:57 PM Charles Stover ***@***.***> wrote:
That quote doesn't seem to be against the pattern used here. It's
unfortunate that it's not a semantic guarantee, but performance takes a
higher priority, as the purpose of passing a static reference to
forceUpdate is to improve pure component performance. If React, in its
wisdom, decides that rerendering pure components is a worthwhile trade for
freed memory, this hook should not hinder that process.
The current implementation does work without useMemo and adds useMemo to
optimize performance. It adds it for the semantics too, but it looks like
the React team decided performance is of higher importance than semantics.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHGvCmxgP6-WpUqlxOc-HoF8puaZuOVnks5vdksYgaJpZM4ccC7g>
.
|
I agree with switching from As for the From the docs:
And it would give the same result as
Except that according to the docs,
|
This still sounds suspicious to me because if these 2 are equivalent, if
Don't very agree with this statement. Even without my something different understanding of what |
While that being true currently, after some digging it seems that
And they also don't want to invalidate useCallback for the wrong reasons facebook/react#14099 (comment), while the reason for I worded the statement about But I might be wrong, my main point is just that going away from |
I asked around, and I'm leaning towards Something along the lines of: useCallback(
() => { dispatch(null); },
[ dispatch ]
); |
Given that this is now in Thank you so much for the discussion and address this issue. |
This way it is shorter.
Also this implementation does not rely on
useMemo
which according to documentation may decide to skip caching under certain circumstances in the future.