Skip to content

Commit 432391c

Browse files
committed
Adds deprecation warning for ReactDOM.unstable_createPortal
1 parent 6e258c1 commit 432391c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/react-dom/src/__tests__/ReactDOMFiber-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ describe('ReactDOMFiber', () => {
222222

223223
// TODO: remove in React 17
224224
it('should support unstable_createPortal alias', () => {
225+
spyOnDev(console, 'warn');
225226
const portalContainer = document.createElement('div');
226227

227228
ReactDOM.render(
@@ -233,6 +234,16 @@ describe('ReactDOMFiber', () => {
233234
expect(portalContainer.innerHTML).toBe('<div>portal</div>');
234235
expect(container.innerHTML).toBe('<div></div>');
235236

237+
if (__DEV__) {
238+
expect(console.warn.calls.count()).toBe(1);
239+
expect(console.warn.calls.argsFor(0)[0]).toContain(
240+
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
241+
'and will be removed in React 17+. Update your code to use ' +
242+
'ReactDOM.createPortal() instead. It has the exact same API, ' +
243+
'but without the "unstable_" prefix.',
244+
);
245+
}
246+
236247
ReactDOM.unmountComponentAtNode(container);
237248
expect(portalContainer.innerHTML).toBe('');
238249
expect(container.innerHTML).toBe('');

packages/react-dom/src/client/ReactDOM.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const {precacheFiberNode, updateFiberProps} = ReactDOMComponentTree;
7575
let SUPPRESS_HYDRATION_WARNING;
7676
let topLevelUpdateWarnings;
7777
let warnOnInvalidCallback;
78+
let didWarnAboutUnstableCreatePortal = false;
7879

7980
if (__DEV__) {
8081
SUPPRESS_HYDRATION_WARNING = 'suppressHydrationWarning';
@@ -1276,7 +1277,19 @@ const ReactDOM: Object = {
12761277

12771278
// Temporary alias since we already shipped React 16 RC with it.
12781279
// TODO: remove in React 17.
1279-
unstable_createPortal: createPortal,
1280+
unstable_createPortal(...args) {
1281+
if (!didWarnAboutUnstableCreatePortal) {
1282+
didWarnAboutUnstableCreatePortal = true;
1283+
lowPriorityWarning(
1284+
false,
1285+
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
1286+
'and will be removed in React 17+. Update your code to use ' +
1287+
'ReactDOM.createPortal() instead. It has the exact same API, ' +
1288+
'but without the "unstable_" prefix.',
1289+
);
1290+
}
1291+
return createPortal(...args);
1292+
},
12801293

12811294
unstable_batchedUpdates: ReactGenericBatching.batchedUpdates,
12821295

0 commit comments

Comments
 (0)