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

fix: avoid startTransition in React Native #178

Merged
merged 3 commits into from
Oct 31, 2024

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented Oct 30, 2024

About the changes

Unleash SDK uses startTransition under the hood. RN 0.75 and 0.76 (latest) break on startTransition (facebook/react-native#44586). React itself removed internal _callbacks set on the transition (acdlite/react@12a387d) while RN still has it.

This PR performs feature detection to find if the code is running in RN and skips startTransition in a similar way as we do for older React versions.

Ideally this PR should not be needed but until RN supports the new startTransition we may need to have this fallback.

Important files

Discussion points

@@ -23,7 +23,11 @@ const offlineConfig: IConfig = {
// save startTransition as var to avoid webpack analysis (https://github.com/webpack/webpack/issues/14814)
const _startTransition = 'startTransition';
// fallback for React <18 which doesn't support startTransition
const startTransition = React[_startTransition] || (fn => fn());
const isReactNative = typeof navigator !== 'undefined' && navigator?.product === 'ReactNative';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this detection may not work in every ReactNative context I'm also considering config option disableTransitions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startTransition?: boolean on provider makes sense. With useCallback inside provider.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to make an automatic detection that doesn't work all the time? If that's the case I think a config option is a better approach.

What do we use this hook for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook was added to solve some hydration error: #125

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also prefer config over imperfect feature detection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tymek your proposal seems fine with optional startTransition. But maybe instead of boolean config we can allow code injection instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startTransition={fn => fn()} would solve the problem in the user space

@@ -23,7 +23,11 @@ const offlineConfig: IConfig = {
// save startTransition as var to avoid webpack analysis (https://github.com/webpack/webpack/issues/14814)
const _startTransition = 'startTransition';
// fallback for React <18 which doesn't support startTransition
const startTransition = React[_startTransition] || (fn => fn());
const isReactNative = typeof navigator !== 'undefined' && navigator?.product === 'ReactNative';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startTransition?: boolean on provider makes sense. With useCallback inside provider.

Copy link
Contributor

@FredrikOseberg FredrikOseberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use config option instead of using an automatic detection that might not work.

@kwasniew kwasniew merged commit 1d1d3aa into main Oct 31, 2024
4 checks passed
@kwasniew kwasniew deleted the avoid-start-transition-react-native branch October 31, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants