Skip to content
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

Support for async loading in Remix/Esbuild #2546

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ryanwilsonperkin
Copy link
Member

Description

Works together with #2538

When attempting to lazy load React components and GraphQL documents using Remix/Esbuild, we've found that the load callback from the async resolver will often be fired while the useMountedRef is returning false. This might indicate an edge case in useMountedRef, potentially because of the use of ESM in the browser, or rendering under Strict Mode.

This meant that the callback would be triggered and would resolve the async value, but because the mounted ref was set to false, we would not call the setState and so would never show the loaded value. By removing the mounted check, we get the value setting correctly.

We also believe that this is the right path forward regardless of this loading bug, because of a change introduced in React 18 to no longer warn in this scenario. Setting state when the component was unmounted would previously emit a warning, with the intent to get developers to cancel their async actions as a cleanup state when unmounting a component. Unfortunately, many async actions don't have an easy option for cancelling so it led many devs to create variants of isMounted

1. React no longer recommends this pattern and has removed the warning
   about "set state after unmounted may indicate a memory leak" because
   the intention was to help developers find places to cancel
   asynchronous actions but many can't be cancelled so this just leads
   to noise in the code.

2. This led to race conditions where a load() call is made while mounted
   is still false and the loaded result is never set in the value. The
   callback is never invoked a second time, so the value never gets set
   despite the load completing.
1. React no longer recommends this pattern and has removed the warning
about "set state after unmounted may indicate a memory leak" because
the intention was to help developers find places to cancel
asynchronous actions but many can't be cancelled so this just leads
to noise in the code.

2. This led to race conditions where a load() call is made while mounted
is still false and the loaded result is never set in the value. The
callback is never invoked a second time, so the value never gets set
despite the load completing.
@ryanwilsonperkin
Copy link
Member Author

/snapit

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

🫰✨ Thanks @ryanwilsonperkin! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/browser@0.0.0-snapshot-20230203161211
yarn add @shopify/react-async@0.0.0-snapshot-20230203161211
yarn add @shopify/react-graphql@0.0.0-snapshot-20230203161211
yarn add @shopify/react-graphql-universal-provider@0.0.0-snapshot-20230203161211
yarn add @shopify/react-i18n@0.0.0-snapshot-20230203161211
yarn add @shopify/react-i18n-universal-provider@0.0.0-snapshot-20230203161211
yarn add @shopify/react-server@0.0.0-snapshot-20230203161211
yarn add @shopify/react-testing@0.0.0-snapshot-20230203161211

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.

None yet

1 participant