Skip to content

Image: do not restart loading when load callback identities change - #2851

Open
AbdullahAnsarii wants to merge 1 commit into
necolas:masterfrom
AbdullahAnsarii:fix/image-load-callback-identity
Open

AbdullahAnsarii wants to merge 1 commit into
necolas:masterfrom
AbdullahAnsarii:fix/image-load-callback-identity

Conversation

@AbdullahAnsarii

Copy link
Copy Markdown

Fixes #2850.

The image-loading effect depended on onError/onLoad/onLoadEnd/onLoadStart, so new handler identities (inline functions re-created by a parent render) aborted the in-flight request, reset state to LOADING, and re-fired onLoadStart for an unchanged uri — looping indefinitely when a handler sets state (live repro in the issue). This reads the handlers through a ref that is refreshed on every render: the effect depends only on the uri, while the handlers invoked are always the ones from the most recent render. That matches native, where handler identity never restarts a load, and follows the same principle as depending on the resolved uri string rather than the source object.

Two tests added:

  • a regression test: re-rendering with new handler identities for the same uri must not abort the request, start a second load, or re-fire onLoadStart (fails on master);
  • a semantics test: when a load settles after a re-render, the latest handlers are invoked.

They're placed at the end of the suite deliberately — inserting tests mid-file shifts the module-level _filterId sequence and breaks unrelated style snapshots.

jest (35/35, snapshots 27/27), prettier, eslint, and flow all pass.

The image-loading effect listed onError, onLoad, onLoadEnd, and
onLoadStart in its dependency array. Passing inline handlers - new
function identities on every parent render - made the effect re-run
for an unchanged uri: the cleanup aborted the in-flight request, state
was reset to LOADING, onLoadStart fired again, and the load restarted.
A handler such as onLoadEnd={() => setLoading(false)} triggers a
re-render and can loop this indefinitely, with the element never
settling even though the browser has the image fully decoded.

Read the handlers through a ref that is refreshed on every render, so
the effect depends only on the uri while the handlers invoked are
always the ones from the most recent render. Native Image is unaffected
by handler identity, so this also aligns web behavior with native.
@codesandbox-ci

Copy link
Copy Markdown

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 71e3287:

Sandbox Source
react-native-web-examples Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image: changing load callback identity aborts and restarts the image load

1 participant