Skip to content

Commit ce030c0

Browse files
committed
Rename to withSuspenseConfig and drop the default config
This allow opting out of suspending in some nested scope. A lot of time when you use this function you'll use it with high level helpers. Those helpers often want to accept some additional configuration for suspense and if it should suspend at all. The easiest way is to just have the api accept null or a suspense config and pass it through. However, then you have to remember that calling suspendIfNeeded has a default. It gets simpler by just saying tat you can pass the config. You can have your own default in user space.
1 parent 919826e commit ce030c0

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,12 @@ describe('ReactSuspenseWithNoopRenderer', () => {
745745
expect(Scheduler).toFlushAndYield(['S']);
746746

747747
// Schedule an update, and suspend for up to 5 seconds.
748-
React.unstable_suspendIfNeeded(() => ReactNoop.render(<App text="A" />), {
749-
timeoutMs: 5000,
750-
});
748+
React.unstable_withSuspenseConfig(
749+
() => ReactNoop.render(<App text="A" />),
750+
{
751+
timeoutMs: 5000,
752+
},
753+
);
751754
// The update should suspend.
752755
expect(Scheduler).toFlushAndYield(['Suspend! [A]', 'Loading...']);
753756
expect(ReactNoop.getChildren()).toEqual([span('S')]);
@@ -759,9 +762,12 @@ describe('ReactSuspenseWithNoopRenderer', () => {
759762
expect(ReactNoop.getChildren()).toEqual([span('S')]);
760763

761764
// Schedule another low priority update.
762-
React.unstable_suspendIfNeeded(() => ReactNoop.render(<App text="B" />), {
763-
timeoutMs: 10000,
764-
});
765+
React.unstable_withSuspenseConfig(
766+
() => ReactNoop.render(<App text="B" />),
767+
{
768+
timeoutMs: 10000,
769+
},
770+
);
765771
// This update should also suspend.
766772
expect(Scheduler).toFlushAndYield(['Suspend! [B]', 'Loading...']);
767773
expect(ReactNoop.getChildren()).toEqual([span('S')]);
@@ -1665,7 +1671,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
16651671
ReactNoop.render(<Foo />);
16661672

16671673
// Took a long time to render. This is to ensure we get a long suspense time.
1668-
// Could also use something like suspendIfNeeded to simulate this.
1674+
// Could also use something like withSuspenseConfig to simulate this.
16691675
Scheduler.advanceTime(1500);
16701676
await advanceTimers(1500);
16711677

packages/react/src/React.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
useRef,
4141
useState,
4242
} from './ReactHooks';
43-
import {suspendIfNeeded} from './ReactBatchConfig';
43+
import {withSuspenseConfig} from './ReactBatchConfig';
4444
import {
4545
createElementWithValidation,
4646
createFactoryWithValidation,
@@ -96,7 +96,7 @@ const React = {
9696

9797
version: ReactVersion,
9898

99-
unstable_suspendIfNeeded: suspendIfNeeded,
99+
unstable_withSuspenseConfig: withSuspenseConfig,
100100

101101
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals,
102102
};

packages/react/src/ReactBatchConfig.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@ import type {SuspenseConfig} from 'react-reconciler/src/ReactFiberSuspenseConfig
1111

1212
import ReactCurrentBatchConfig from './ReactCurrentBatchConfig';
1313

14-
const DefaultSuspenseConfig: SuspenseConfig = {
15-
timeoutMs: 5000,
16-
loadingDelayMs: 0,
17-
minLoadingDurationMs: 0,
18-
};
19-
2014
// Within the scope of the callback, mark all updates as being allowed to suspend.
21-
export function suspendIfNeeded(scope: () => void, config?: SuspenseConfig) {
15+
export function withSuspenseConfig(scope: () => void, config?: SuspenseConfig) {
2216
const previousConfig = ReactCurrentBatchConfig.suspense;
23-
ReactCurrentBatchConfig.suspense = config || DefaultSuspenseConfig;
17+
ReactCurrentBatchConfig.suspense = config === undefined ? null : config;
2418
try {
2519
scope();
2620
} finally {

0 commit comments

Comments
 (0)