Skip to content

Commit

Permalink
Let ReactDOM initialize in RSC (#25503)
Browse files Browse the repository at this point in the history
With the `react-dom/server-rendering-stub` you can import `react-dom` in
RSC so that you can call `preload` and `preinit` but if you don't alias
it, then requiring it breaks because we React.Component which doesn't
exist in the react subset.
  • Loading branch information
sebmarkbage authored Oct 21, 2022
1 parent 1f7a2f5 commit 79c5829
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMInReactServer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its 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';

describe('ReactDOMInReactServer', () => {
beforeEach(() => {
jest.resetModules();
jest.mock('react', () => require('react/react.shared-subset'));
});

// @gate experimental && !www
it('can require react-dom', () => {
// In RSC this will be aliased.
require('react');
require('react-dom');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ const fakeInternalInstance = {};

// React.Component uses a shared frozen object by default.
// We'll use it to determine whether we need to initialize legacy refs.
export const emptyRefsObject: $FlowFixMe = new React.Component().refs;
export const emptyRefsObject: $FlowFixMe = React.Component
? new React.Component().refs
: {};

let didWarnAboutStateAssignmentForComponent;
let didWarnAboutUninitializedState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ const fakeInternalInstance = {};

// React.Component uses a shared frozen object by default.
// We'll use it to determine whether we need to initialize legacy refs.
export const emptyRefsObject: $FlowFixMe = new React.Component().refs;
export const emptyRefsObject: $FlowFixMe = React.Component
? new React.Component().refs
: {};

let didWarnAboutStateAssignmentForComponent;
let didWarnAboutUninitializedState;
Expand Down
10 changes: 10 additions & 0 deletions packages/react/react.shared-subset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from './src/ReactSharedSubset';
8 changes: 8 additions & 0 deletions scripts/jest/setupHostConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ jest.mock('react', () => {
return jest.requireActual(resolvedEntryPoint);
});

jest.mock('react/react.shared-subset', () => {
const resolvedEntryPoint = resolveEntryFork(
require.resolve('react/src/ReactSharedSubset'),
global.__WWW__
);
return jest.requireActual(resolvedEntryPoint);
});

jest.mock('react-reconciler/src/ReactFiberReconciler', () => {
return jest.requireActual(
__VARIANT__
Expand Down

0 comments on commit 79c5829

Please sign in to comment.