Skip to content

Commit

Permalink
fix: fallback for startTransition for React <18 (#152)
Browse files Browse the repository at this point in the history
Fixes #151
  • Loading branch information
jameshartig authored Nov 27, 2023
1 parent 874602e commit 8813e5b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/FlagProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const offlineConfig: IConfig = {
clientKey: 'not-used',
};

// 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 FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
config: customConfig,
children,
Expand Down Expand Up @@ -48,7 +53,7 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
}

const errorCallback = (e: any) => {
React.startTransition(() => {
startTransition(() => {
setFlagsError(currentError => currentError || e);
});
};
Expand All @@ -57,7 +62,7 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
const readyCallback = () => {
// wait for flags to resolve after useFlag gets the same event
timeout = setTimeout(() => {
React.startTransition(() => {
startTransition(() => {
setFlagsReady(true);
});
}, 0);
Expand Down

0 comments on commit 8813e5b

Please sign in to comment.