-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement useDeferredValue initialValue option
Adds a second argument to useDeferredValue called initialValue: ```js const value = useDeferredValue(finalValue, initialValue); ``` During the initial render of a component, useDeferredValue will return initialValue. Once that render finishes, it will spawn an additional render to switch to finalValue. This same sequence should occur whenever the hook is hidden and revealed again, i.e. by a Suspense or Activity, though this part is not yet implemented. When initialValue is not provided, useDeferredValue has no effect during initial render, but during an update, it will remain on the previous value, then spawn an additional render to switch to the new value. During SSR, initialValue is always used, if provided. This feature is currently behind an experimental flag. We plan to ship it in a non-breaking release.
- Loading branch information
Showing
6 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/react-dom/src/__tests__/ReactDOMFizzDeferredValue-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import {insertNodesAndExecuteScripts} from '../test-utils/FizzTestUtils'; | ||
|
||
// Polyfills for test environment | ||
global.ReadableStream = | ||
require('web-streams-polyfill/ponyfill/es6').ReadableStream; | ||
global.TextEncoder = require('util').TextEncoder; | ||
|
||
let act; | ||
let container; | ||
let React; | ||
let ReactDOMServer; | ||
let ReactDOMClient; | ||
let useDeferredValue; | ||
|
||
describe('ReactDOMFizzForm', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOMServer = require('react-dom/server.browser'); | ||
ReactDOMClient = require('react-dom/client'); | ||
useDeferredValue = require('react').useDeferredValue; | ||
act = require('internal-test-utils').act; | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
async function readIntoContainer(stream) { | ||
const reader = stream.getReader(); | ||
let result = ''; | ||
while (true) { | ||
const {done, value} = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
result += Buffer.from(value).toString('utf8'); | ||
} | ||
const temp = document.createElement('div'); | ||
temp.innerHTML = result; | ||
insertNodesAndExecuteScripts(temp, container, null); | ||
} | ||
|
||
// @gate enableUseDeferredValueInitialArg | ||
it('returns initialValue argument, if provided', async () => { | ||
function App() { | ||
return useDeferredValue('Final', 'Initial'); | ||
} | ||
|
||
const stream = await ReactDOMServer.renderToReadableStream(<App />); | ||
await readIntoContainer(stream); | ||
expect(container.textContent).toEqual('Initial'); | ||
|
||
// After hydration, it's updated to the final value | ||
await act(() => ReactDOMClient.hydrateRoot(container, <App />)); | ||
expect(container.textContent).toEqual('Final'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters