mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Allow returning a temporary reference inside an async function (#33761)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary Fixes `await`-ing and returning temporary references in `async` functions. These two operations invoke `.then()` under the hood if it is available, which currently results in an "Cannot access then on the server. You cannot dot into a temporary client reference..." error. This can easily be reproduced by returning a temporary reference from a server function. Fixes #33534 ## How did you test this change? I added a test in a new test file. I wasn't sure where else to put it. <img width="771" height="138" alt="image" src="https://github.com/user-attachments/assets/09ffe6eb-271a-4842-a9fe-c68e17b3fb41" /> <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. -->
This commit is contained in:
parent
ddf8bc3fba
commit
1d163962b2
|
|
@ -70,6 +70,9 @@ const proxyHandlers = {
|
|||
`Instead, you can export a Client Component wrapper ` +
|
||||
`that itself renders a Client Context Provider.`,
|
||||
);
|
||||
// Allow returning a temporary reference from an async function
|
||||
case 'then':
|
||||
return undefined;
|
||||
}
|
||||
throw new Error(
|
||||
// eslint-disable-next-line react-internal/safe-string-coercion
|
||||
|
|
|
|||
36
packages/react-server/src/__tests__/ReactFlightServerTemporaryReferences-test.js
vendored
Normal file
36
packages/react-server/src/__tests__/ReactFlightServerTemporaryReferences-test.js
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* 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
|
||||
* @jest-environment node
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
let ReactFlightServerTemporaryReferences;
|
||||
|
||||
describe('ReactFlightServerTemporaryReferences', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
ReactFlightServerTemporaryReferences = require('react-server/src/ReactFlightServerTemporaryReferences');
|
||||
});
|
||||
|
||||
it('can return a temporary reference from an async function', async () => {
|
||||
const temporaryReferenceSet =
|
||||
ReactFlightServerTemporaryReferences.createTemporaryReferenceSet();
|
||||
const temporaryReference =
|
||||
ReactFlightServerTemporaryReferences.createTemporaryReference(
|
||||
temporaryReferenceSet,
|
||||
'test',
|
||||
);
|
||||
|
||||
async function foo() {
|
||||
return temporaryReference;
|
||||
}
|
||||
|
||||
await expect(foo()).resolves.toBe(temporaryReference);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user