Skip to content

Commit f2fd484

Browse files
authored
Add React.createFactory() deprecation warning (#17878)
1 parent 0c04aca commit f2fd484

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

packages/react-is/src/__tests__/ReactIs-test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,16 @@ describe('ReactIs', () => {
5757
expect(ReactIs.isValidElementType(Context.Provider)).toEqual(true);
5858
expect(ReactIs.isValidElementType(Context.Consumer)).toEqual(true);
5959
if (!ReactFeatureFlags.disableCreateFactory) {
60-
expect(ReactIs.isValidElementType(React.createFactory('div'))).toEqual(
61-
true,
60+
let factory;
61+
expect(() => {
62+
factory = React.createFactory('div');
63+
}).toWarnDev(
64+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
65+
'future major release. Consider using JSX or use React.createElement() ' +
66+
'directly instead.',
67+
{withoutStack: true},
6268
);
69+
expect(ReactIs.isValidElementType(factory)).toEqual(true);
6370
}
6471
expect(ReactIs.isValidElementType(React.Fragment)).toEqual(true);
6572
expect(ReactIs.isValidElementType(React.StrictMode)).toEqual(true);

packages/react/src/ReactElementValidator.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,21 @@ export function createElementWithValidation(type, props, children) {
473473
return element;
474474
}
475475

476+
let didWarnAboutDeprecatedCreateFactory = false;
477+
476478
export function createFactoryWithValidation(type) {
477479
const validatedFactory = createElementWithValidation.bind(null, type);
478480
validatedFactory.type = type;
479-
// Legacy hook: remove it
480481
if (__DEV__) {
482+
if (!didWarnAboutDeprecatedCreateFactory) {
483+
didWarnAboutDeprecatedCreateFactory = true;
484+
console.warn(
485+
'React.createFactory() is deprecated and will be removed in ' +
486+
'a future major release. Consider using JSX ' +
487+
'or use React.createElement() directly instead.',
488+
);
489+
}
490+
// Legacy hook: remove it
481491
Object.defineProperty(validatedFactory, 'type', {
482492
enumerable: false,
483493
get: function() {

packages/react/src/__tests__/ReactElement-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,16 @@ describe('ReactElement', () => {
313313
expect(React.isValidElement({})).toEqual(false);
314314
expect(React.isValidElement('string')).toEqual(false);
315315
if (!ReactFeatureFlags.disableCreateFactory) {
316-
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
316+
let factory;
317+
expect(() => {
318+
factory = React.createFactory('div');
319+
}).toWarnDev(
320+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
321+
'future major release. Consider using JSX or use React.createElement() ' +
322+
'directly instead.',
323+
{withoutStack: true},
324+
);
325+
expect(React.isValidElement(factory)).toEqual(false);
317326
}
318327
expect(React.isValidElement(Component)).toEqual(false);
319328
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
@@ -473,7 +482,16 @@ describe('ReactElement', () => {
473482
expect(React.isValidElement({})).toEqual(false);
474483
expect(React.isValidElement('string')).toEqual(false);
475484
if (!ReactFeatureFlags.disableCreateFactory) {
476-
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
485+
let factory;
486+
expect(() => {
487+
factory = React.createFactory('div');
488+
}).toWarnDev(
489+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
490+
'future major release. Consider using JSX or use React.createElement() ' +
491+
'directly instead.',
492+
{withoutStack: true},
493+
);
494+
expect(React.isValidElement(factory)).toEqual(false);
477495
}
478496
expect(React.isValidElement(Component)).toEqual(false);
479497
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);

packages/react/src/__tests__/ReactElementJSX-test.internal.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ describe('ReactElement.jsx', () => {
6969
expect(React.isValidElement({})).toEqual(false);
7070
expect(React.isValidElement('string')).toEqual(false);
7171
if (!ReactFeatureFlags.disableCreateFactory) {
72-
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
72+
let factory;
73+
expect(() => {
74+
factory = React.createFactory('div');
75+
}).toWarnDev(
76+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
77+
'future major release. Consider using JSX or use React.createElement() ' +
78+
'directly instead.',
79+
{withoutStack: true},
80+
);
81+
expect(React.isValidElement(factory)).toEqual(false);
7382
}
7483
expect(React.isValidElement(Component)).toEqual(false);
7584
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
@@ -301,7 +310,16 @@ describe('ReactElement.jsx', () => {
301310
expect(React.isValidElement({})).toEqual(false);
302311
expect(React.isValidElement('string')).toEqual(false);
303312
if (!ReactFeatureFlags.disableCreateFactory) {
304-
expect(React.isValidElement(React.createFactory('div'))).toEqual(false);
313+
let factory;
314+
expect(() => {
315+
factory = React.createFactory('div');
316+
}).toWarnDev(
317+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
318+
'future major release. Consider using JSX or use React.createElement() ' +
319+
'directly instead.',
320+
{withoutStack: true},
321+
);
322+
expect(React.isValidElement(factory)).toEqual(false);
305323
}
306324
expect(React.isValidElement(Component)).toEqual(false);
307325
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);

packages/react/src/__tests__/ReactElementValidator-test.internal.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,17 @@ describe('ReactElementValidator', () => {
445445
return <div />;
446446
}
447447

448-
let TestFactory = React.createFactory(TestComponent);
448+
let TestFactory;
449+
450+
expect(() => {
451+
TestFactory = React.createFactory(TestComponent);
452+
}).toWarnDev(
453+
'Warning: React.createFactory() is deprecated and will be removed in a ' +
454+
'future major release. Consider using JSX or use React.createElement() ' +
455+
'directly instead.',
456+
{withoutStack: true},
457+
);
458+
449459
expect(
450460
() => TestFactory.type,
451461
).toWarnDev(

0 commit comments

Comments
 (0)