-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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(Image): keep function identiy accross renders #2638
base: master
Are you sure you want to change the base?
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9e05b48:
|
@necolas Does the fix make sense? I didn't know where to put the utility method so added in vendor to get feedback, but tell me where the right place is please |
const callbackRef = useRef(callback); | ||
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect; | ||
|
||
useIsomorphicLayoutEffect(() => { |
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.
Is there any point delaying the ref update through an effect?
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.
initial ref is set with const callbackRef = useRef(callback);
it then updates in the effect
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 understand that. I mean, you can simply update the ref without any effects. Simpler code and it guarantees that the most recent value of callback
is assigned to the ref and will be called by Image - you don't know when exactly the effect will be executed and Image may stop loading between the render and the effect.
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 true. I'm wondering why swr
does it this way though? (They also don't have the config in the dependency array). There's also the experimental_useEffectEvent
from react, so I guess there are edge cases with both solutions
Description
Currently when an inline function is passed to the
Image
orAnimated.Image
component, theuseEffect
will be triggered on every render, causing the image the be fetched on every render.props accepting a function and declared in the dependency array are:
onLoad
onLoadEnd
onLoadStart
onLoadError
.It shouldn't be up to the developer to pass a memoized function.
The browser will cache subsequent image fetches (not sure about all platforms), so as it may seem acceptable setting the cache key to
reload
will fetch the image on every render.Example