Skip to content

Commit 5f4b0c9

Browse files
committed
Enable concurrent mode by using createRoot to mount editor apps
1 parent 7c4fe12 commit 5f4b0c9

File tree

6 files changed

+19
-99
lines changed

6 files changed

+19
-99
lines changed

packages/edit-post/README.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -476,20 +476,6 @@ _Returns_
476476

477477
- `WPComponent`: The component to be rendered.
478478

479-
### reinitializeEditor
480-
481-
Reinitializes the editor after the user chooses to reboot the editor after
482-
an unhandled error occurs, replacing previously mounted editor element using
483-
an initial state from prior to the crash.
484-
485-
_Parameters_
486-
487-
- _postType_ `Object`: Post type of the post to edit.
488-
- _postId_ `Object`: ID of the post to edit.
489-
- _target_ `Element`: DOM node in which editor is rendered.
490-
- _settings_ `?Object`: Editor settings object.
491-
- _initialEdits_ `Object`: Programmatic edits to apply initially, to be considered as non-user-initiated (bypass for unsaved changes prompt).
492-
493479
### store
494480

495481
Store definition for the edit post namespace.

packages/edit-post/src/index.js

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
registerCoreBlocks,
77
__experimentalRegisterExperimentalCoreBlocks,
88
} from '@wordpress/block-library';
9-
import { render, unmountComponentAtNode } from '@wordpress/element';
9+
import { createRoot } from '@wordpress/element';
1010
import { dispatch, select } from '@wordpress/data';
1111
import { addFilter } from '@wordpress/hooks';
1212
import { store as preferencesStore } from '@wordpress/preferences';
@@ -19,49 +19,6 @@ import './plugins';
1919
import Editor from './editor';
2020
import { store as editPostStore } from './store';
2121

22-
/**
23-
* Reinitializes the editor after the user chooses to reboot the editor after
24-
* an unhandled error occurs, replacing previously mounted editor element using
25-
* an initial state from prior to the crash.
26-
*
27-
* @param {Object} postType Post type of the post to edit.
28-
* @param {Object} postId ID of the post to edit.
29-
* @param {Element} target DOM node in which editor is rendered.
30-
* @param {?Object} settings Editor settings object.
31-
* @param {Object} initialEdits Programmatic edits to apply initially, to be
32-
* considered as non-user-initiated (bypass for
33-
* unsaved changes prompt).
34-
*/
35-
export function reinitializeEditor(
36-
postType,
37-
postId,
38-
target,
39-
settings,
40-
initialEdits
41-
) {
42-
unmountComponentAtNode( target );
43-
const reboot = reinitializeEditor.bind(
44-
null,
45-
postType,
46-
postId,
47-
target,
48-
settings,
49-
initialEdits
50-
);
51-
52-
render(
53-
<Editor
54-
settings={ settings }
55-
onError={ reboot }
56-
postId={ postId }
57-
postType={ postType }
58-
initialEdits={ initialEdits }
59-
recovery
60-
/>,
61-
target
62-
);
63-
}
64-
6522
/**
6623
* Initializes and returns an instance of Editor.
6724
*
@@ -81,14 +38,6 @@ export function initializeEditor(
8138
initialEdits
8239
) {
8340
const target = document.getElementById( id );
84-
const reboot = reinitializeEditor.bind(
85-
null,
86-
postType,
87-
postId,
88-
target,
89-
settings,
90-
initialEdits
91-
);
9241

9342
dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
9443
editorMode: 'visual',
@@ -185,15 +134,13 @@ export function initializeEditor(
185134
window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false );
186135
window.addEventListener( 'drop', ( e ) => e.preventDefault(), false );
187136

188-
render(
137+
createRoot( target ).render(
189138
<Editor
190139
settings={ settings }
191-
onError={ reboot }
192140
postId={ postId }
193141
postType={ postType }
194142
initialEdits={ initialEdits }
195-
/>,
196-
target
143+
/>
197144
);
198145
}
199146

packages/edit-site/src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
__experimentalRegisterExperimentalCoreBlocks,
88
} from '@wordpress/block-library';
99
import { dispatch } from '@wordpress/data';
10-
import { render, unmountComponentAtNode } from '@wordpress/element';
10+
import { createRoot } from '@wordpress/element';
1111
import {
1212
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
1313
__experimentalFetchUrlData as fetchUrlData,
@@ -53,10 +53,6 @@ export function reinitializeEditor( target, settings ) {
5353
}
5454
);
5555

56-
// This will be a no-op if the target doesn't have any React nodes.
57-
unmountComponentAtNode( target );
58-
const reboot = reinitializeEditor.bind( null, target, settings );
59-
6056
// We dispatch actions and update the store synchronously before rendering
6157
// so that we won't trigger unnecessary re-renders with useEffect.
6258
{
@@ -91,7 +87,7 @@ export function reinitializeEditor( target, settings ) {
9187
window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false );
9288
window.addEventListener( 'drop', ( e ) => e.preventDefault(), false );
9389

94-
render( <App reboot={ reboot } />, target );
90+
createRoot( target ).render( <App /> );
9591
}
9692

9793
/**

packages/edit-widgets/src/index.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
store as blocksStore,
99
} from '@wordpress/blocks';
1010
import { dispatch } from '@wordpress/data';
11-
import { render, unmountComponentAtNode } from '@wordpress/element';
11+
import { createRoot } from '@wordpress/element';
1212
import {
1313
registerCoreBlocks,
1414
__experimentalGetCoreBlocks,
@@ -42,23 +42,6 @@ const disabledBlocks = [
4242
...( ALLOW_REUSABLE_BLOCKS ? [] : [ 'core/block' ] ),
4343
];
4444

45-
/**
46-
* Reinitializes the editor after the user chooses to reboot the editor after
47-
* an unhandled error occurs, replacing previously mounted editor element using
48-
* an initial state from prior to the crash.
49-
*
50-
* @param {Element} target DOM node in which editor is rendered.
51-
* @param {?Object} settings Editor settings object.
52-
*/
53-
export function reinitializeEditor( target, settings ) {
54-
unmountComponentAtNode( target );
55-
const reboot = reinitializeEditor.bind( null, target, settings );
56-
render(
57-
<Layout blockEditorSettings={ settings } onError={ reboot } />,
58-
target
59-
);
60-
}
61-
6245
/**
6346
* Initializes the block editor in the widgets screen.
6447
*
@@ -67,7 +50,6 @@ export function reinitializeEditor( target, settings ) {
6750
*/
6851
export function initialize( id, settings ) {
6952
const target = document.getElementById( id );
70-
const reboot = reinitializeEditor.bind( null, target, settings );
7153
const coreBlocks = __experimentalGetCoreBlocks().filter( ( block ) => {
7254
return ! (
7355
disabledBlocks.includes( block.name ) ||
@@ -105,10 +87,7 @@ export function initialize( id, settings ) {
10587
// do this will result in errors in the default block parser.
10688
// see: https://github.com/WordPress/gutenberg/issues/33097
10789
setFreeformContentHandlerName( 'core/html' );
108-
render(
109-
<Layout blockEditorSettings={ settings } onError={ reboot } />,
110-
target
111-
);
90+
createRoot( target ).render( <Layout blockEditorSettings={ settings } /> );
11291
}
11392

11493
/**

packages/element/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ _Returns_
198198

199199
- `Object`: Ref object.
200200

201+
### createRoot
202+
203+
Undocumented declaration.
204+
201205
### findDOMNode
202206

203207
Finds the dom node of a React component.
@@ -234,6 +238,10 @@ _Parameters_
234238
- _element_ `import('./react').WPElement`: Element to hydrate.
235239
- _target_ `HTMLElement`: DOM node into which element should be hydrated.
236240

241+
### hydrateRoot
242+
243+
Undocumented declaration.
244+
237245
### isEmptyElement
238246

239247
Checks if the provided WP element is empty.

packages/element/src/react-platform.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
hydrate,
99
unmountComponentAtNode,
1010
} from 'react-dom';
11+
import { createRoot, hydrateRoot } from 'react-dom/client';
1112

1213
/**
1314
* Creates a portal into which a component can be rendered.
@@ -27,6 +28,9 @@ export { createPortal };
2728
*/
2829
export { findDOMNode };
2930

31+
export { createRoot };
32+
33+
export { hydrateRoot };
3034
/**
3135
* Renders a given element into the target DOM node.
3236
*

0 commit comments

Comments
 (0)