GeezerVison is showing github.com — Add "hydrationOptions" behind the enableSuspenseCallback flag (#16434) · react/react@c80678c
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-art/src/ReactART.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,7 +66,7 @@ class Surface extends React.Component { | 66 | 66 |
| 67 | 67 | this._surface = Mode.Surface(+width, +height, this._tagRef); | 68 | 68 |
| 69 | | - this._mountNode = createContainer(this._surface, LegacyRoot, false); | | 69 | + this._mountNode = createContainer(this._surface, LegacyRoot, false, null); | 70 | 70 | updateContainer(this.props.children, this._mountNode, this); | 71 | 71 | } | 72 | 72 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js +148Lines changed: 148 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ describe('ReactDOMServerPartialHydration', () => { | 24 | 24 |
| 25 | 25 | ReactFeatureFlags = require('shared/ReactFeatureFlags'); | 26 | 26 | ReactFeatureFlags.enableSuspenseServerRenderer = true; | | 27 | + ReactFeatureFlags.enableSuspenseCallback = true; | 27 | 28 |
| 28 | 29 | React = require('react'); | 29 | 30 | ReactDOM = require('react-dom'); | @@ -92,6 +93,153 @@ describe('ReactDOMServerPartialHydration', () => { | 92 | 93 | expect(ref.current).toBe(span); | 93 | 94 | }); | 94 | 95 |
| | 96 | + it('calls the hydration callbacks after hydration or deletion', async () => { | | 97 | + let suspend = false; | | 98 | + let resolve; | | 99 | + let promise = new Promise(resolvePromise => (resolve = resolvePromise)); | | 100 | + function Child() { | | 101 | + if (suspend) { | | 102 | + throw promise; | | 103 | + } else { | | 104 | + return 'Hello'; | | 105 | + } | | 106 | + } | | 107 | + | | 108 | + let suspend2 = false; | | 109 | + let promise2 = new Promise(() => {}); | | 110 | + function Child2() { | | 111 | + if (suspend2) { | | 112 | + throw promise2; | | 113 | + } else { | | 114 | + return 'World'; | | 115 | + } | | 116 | + } | | 117 | + | | 118 | + function App({value}) { | | 119 | + return ( | | 120 | + <div> | | 121 | + <Suspense fallback="Loading..."> | | 122 | + <Child /> | | 123 | + </Suspense> | | 124 | + <Suspense fallback="Loading..."> | | 125 | + <Child2 value={value} /> | | 126 | + </Suspense> | | 127 | + </div> | | 128 | + ); | | 129 | + } | | 130 | + | | 131 | + // First we render the final HTML. With the streaming renderer | | 132 | + // this may have suspense points on the server but here we want | | 133 | + // to test the completed HTML. Don't suspend on the server. | | 134 | + suspend = false; | | 135 | + suspend2 = false; | | 136 | + let finalHTML = ReactDOMServer.renderToString(<App />); | | 137 | + | | 138 | + let container = document.createElement('div'); | | 139 | + container.innerHTML = finalHTML; | | 140 | + | | 141 | + let hydrated = []; | | 142 | + let deleted = []; | | 143 | + | | 144 | + // On the client we don't have all data yet but we want to start | | 145 | + // hydrating anyway. | | 146 | + suspend = true; | | 147 | + suspend2 = true; | | 148 | + let root = ReactDOM.unstable_createRoot(container, { | | 149 | + hydrate: true, | | 150 | + hydrationOptions: { | | 151 | + onHydrated(node) { | | 152 | + hydrated.push(node); | | 153 | + }, | | 154 | + onDeleted(node) { | | 155 | + deleted.push(node); | | 156 | + }, | | 157 | + }, | | 158 | + }); | | 159 | + act(() => { | | 160 | + root.render(<App />); | | 161 | + }); | | 162 | + | | 163 | + expect(hydrated.length).toBe(0); | | 164 | + expect(deleted.length).toBe(0); | | 165 | + | | 166 | + await act(async () => { | | 167 | + // Resolving the promise should continue hydration | | 168 | + suspend = false; | | 169 | + resolve(); | | 170 | + await promise; | | 171 | + }); | | 172 | + | | 173 | + expect(hydrated.length).toBe(1); | | 174 | + expect(deleted.length).toBe(0); | | 175 | + | | 176 | + // Performing an update should force it to delete the boundary | | 177 | + root.render(<App value={true} />); | | 178 | + | | 179 | + Scheduler.unstable_flushAll(); | | 180 | + jest.runAllTimers(); | | 181 | + | | 182 | + expect(hydrated.length).toBe(1); | | 183 | + expect(deleted.length).toBe(1); | | 184 | + }); | | 185 | + | | 186 | + it('calls the onDeleted hydration callback if the parent gets deleted', async () => { | | 187 | + let suspend = false; | | 188 | + let promise = new Promise(() => {}); | | 189 | + function Child() { | | 190 | + if (suspend) { | | 191 | + throw promise; | | 192 | + } else { | | 193 | + return 'Hello'; | | 194 | + } | | 195 | + } | | 196 | + | | 197 | + function App({deleted}) { | | 198 | + if (deleted) { | | 199 | + return null; | | 200 | + } | | 201 | + return ( | | 202 | + <div> | | 203 | + <Suspense fallback="Loading..."> | | 204 | + <Child /> | | 205 | + </Suspense> | | 206 | + </div> | | 207 | + ); | | 208 | + } | | 209 | + | | 210 | + suspend = false; | | 211 | + let finalHTML = ReactDOMServer.renderToString(<App />); | | 212 | + | | 213 | + let container = document.createElement('div'); | | 214 | + container.innerHTML = finalHTML; | | 215 | + | | 216 | + let deleted = []; | | 217 | + | | 218 | + // On the client we don't have all data yet but we want to start | | 219 | + // hydrating anyway. | | 220 | + suspend = true; | | 221 | + let root = ReactDOM.unstable_createRoot(container, { | | 222 | + hydrate: true, | | 223 | + hydrationOptions: { | | 224 | + onDeleted(node) { | | 225 | + deleted.push(node); | | 226 | + }, | | 227 | + }, | | 228 | + }); | | 229 | + act(() => { | | 230 | + root.render(<App />); | | 231 | + }); | | 232 | + | | 233 | + expect(deleted.length).toBe(0); | | 234 | + | | 235 | + act(() => { | | 236 | + root.render(<App deleted={true} />); | | 237 | + }); | | 238 | + | | 239 | + // The callback should have been invoked. | | 240 | + expect(deleted.length).toBe(1); | | 241 | + }); | | 242 | + | 95 | 243 | it('warns and replaces the boundary content in legacy mode', async () => { | 96 | 244 | let suspend = false; | 97 | 245 | let resolve; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOM.js +30-9Lines changed: 30 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -367,15 +367,26 @@ ReactWork.prototype._onCommit = function(): void { | 367 | 367 | function ReactSyncRoot( | 368 | 368 | container: DOMContainer, | 369 | 369 | tag: RootTag, | 370 | | - hydrate: boolean, | | 370 | + options: void | RootOptions, | 371 | 371 | ) { | 372 | 372 | // Tag is either LegacyRoot or Concurrent Root | 373 | | - const root = createContainer(container, tag, hydrate); | | 373 | + const hydrate = options != null && options.hydrate === true; | | 374 | + const hydrationCallbacks = | | 375 | + (options != null && options.hydrationOptions) || null; | | 376 | + const root = createContainer(container, tag, hydrate, hydrationCallbacks); | 374 | 377 | this._internalRoot = root; | 375 | 378 | } | 376 | 379 |
| 377 | | -function ReactRoot(container: DOMContainer, hydrate: boolean) { | 378 | | - const root = createContainer(container, ConcurrentRoot, hydrate); | | 380 | +function ReactRoot(container: DOMContainer, options: void | RootOptions) { | | 381 | + const hydrate = options != null && options.hydrate === true; | | 382 | + const hydrationCallbacks = | | 383 | + (options != null && options.hydrationOptions) || null; | | 384 | + const root = createContainer( | | 385 | + container, | | 386 | + ConcurrentRoot, | | 387 | + hydrate, | | 388 | + hydrationCallbacks, | | 389 | + ); | 379 | 390 | this._internalRoot = root; | 380 | 391 | } | 381 | 392 |
| @@ -532,7 +543,15 @@ function legacyCreateRootFromDOMContainer( | 532 | 543 | } | 533 | 544 |
| 534 | 545 | // Legacy roots are not batched. | 535 | | - return new ReactSyncRoot(container, LegacyRoot, shouldHydrate); | | 546 | + return new ReactSyncRoot( | | 547 | + container, | | 548 | + LegacyRoot, | | 549 | + shouldHydrate | | 550 | + ? { | | 551 | + hydrate: true, | | 552 | + } | | 553 | + : undefined, | | 554 | + ); | 536 | 555 | } | 537 | 556 |
| 538 | 557 | function legacyRenderSubtreeIntoContainer( | @@ -824,6 +843,10 @@ const ReactDOM: Object = { | 824 | 843 |
| 825 | 844 | type RootOptions = { | 826 | 845 | hydrate?: boolean, | | 846 | + hydrationOptions?: { | | 847 | + onHydrated?: (suspenseNode: Comment) => void, | | 848 | + onDeleted?: (suspenseNode: Comment) => void, | | 849 | + }, | 827 | 850 | }; | 828 | 851 |
| 829 | 852 | function createRoot( | @@ -839,8 +862,7 @@ function createRoot( | 839 | 862 | functionName, | 840 | 863 | ); | 841 | 864 | warnIfReactDOMContainerInDEV(container); | 842 | | - const hydrate = options != null && options.hydrate === true; | 843 | | - return new ReactRoot(container, hydrate); | | 865 | + return new ReactRoot(container, options); | 844 | 866 | } | 845 | 867 |
| 846 | 868 | function createSyncRoot( | @@ -856,8 +878,7 @@ function createSyncRoot( | 856 | 878 | functionName, | 857 | 879 | ); | 858 | 880 | warnIfReactDOMContainerInDEV(container); | 859 | | - const hydrate = options != null && options.hydrate === true; | 860 | | - return new ReactSyncRoot(container, BatchedRoot, hydrate); | | 881 | + return new ReactSyncRoot(container, BatchedRoot, options); | 861 | 882 | } | 862 | 883 |
| 863 | 884 | function warnIfReactDOMContainerInDEV(container) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabric.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -144,7 +144,7 @@ const ReactFabric: ReactFabricType = { | 144 | 144 | if (!root) { | 145 | 145 | // TODO (bvaughn): If we decide to keep the wrapper component, | 146 | 146 | // We could create a wrapper for containerTag as well to reduce special casing. | 147 | | - root = createContainer(containerTag, LegacyRoot, false); | | 147 | + root = createContainer(containerTag, LegacyRoot, false, null); | 148 | 148 | roots.set(containerTag, root); | 149 | 149 | } | 150 | 150 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactNativeRenderer.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,7 +141,7 @@ const ReactNativeRenderer: ReactNativeType = { | 141 | 141 | if (!root) { | 142 | 142 | // TODO (bvaughn): If we decide to keep the wrapper component, | 143 | 143 | // We could create a wrapper for containerTag as well to reduce special casing. | 144 | | - root = createContainer(containerTag, LegacyRoot, false); | | 144 | + root = createContainer(containerTag, LegacyRoot, false, null); | 145 | 145 | roots.set(containerTag, root); | 146 | 146 | } | 147 | 147 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/createReactNoop.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -908,7 +908,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 908 | 908 | if (!root) { | 909 | 909 | const container = {rootID: rootID, pendingChildren: [], children: []}; | 910 | 910 | rootContainers.set(rootID, container); | 911 | | - root = NoopRenderer.createContainer(container, tag, false); | | 911 | + root = NoopRenderer.createContainer(container, tag, false, null); | 912 | 912 | roots.set(rootID, root); | 913 | 913 | } | 914 | 914 | return root.current.stateNode.containerInfo; | @@ -925,6 +925,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 925 | 925 | container, | 926 | 926 | ConcurrentRoot, | 927 | 927 | false, | | 928 | + null, | 928 | 929 | ); | 929 | 930 | return { | 930 | 931 | _Scheduler: Scheduler, | @@ -950,6 +951,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 950 | 951 | container, | 951 | 952 | BatchedRoot, | 952 | 953 | false, | | 954 | + null, | 953 | 955 | ); | 954 | 956 | return { | 955 | 957 | _Scheduler: Scheduler, | |
|