@samitouri / QOS-React / commits / f2fd484afd

Add React.createFactory() deprecation warning (#17878)

Dominic Gannaway committed Jan 21, 2020 at 15:51 UTC f2fd484afdee1e4e25ee453bb7a544fa0558d172
5 files changed +71 -8
packages/react-is/src/__tests__/ReactIs-test.js
+9 -2
@@ -57,9 +57,16 @@ describe('ReactIs', () => {
57 expect(ReactIs.isValidElementType(Context.Provider)).toEqual(true);
58 expect(ReactIs.isValidElementType(Context.Consumer)).toEqual(true);
59 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},
68 );
69 + expect(ReactIs.isValidElementType(factory)).toEqual(true);
70 }
71 expect(ReactIs.isValidElementType(React.Fragment)).toEqual(true);
72 expect(ReactIs.isValidElementType(React.StrictMode)).toEqual(true);
packages/react/src/ReactElementValidator.js
+11 -1
@@ -473,11 +473,21 @@ export function createElementWithValidation(type, props, children) {
473 return element;
474 }
475
476 +let didWarnAboutDeprecatedCreateFactory = false;
477 +
478 export function createFactoryWithValidation(type) {
479 const validatedFactory = createElementWithValidation.bind(null, type);
480 validatedFactory.type = type;
479 - // Legacy hook: remove it
481 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
491 Object.defineProperty(validatedFactory, 'type', {
492 enumerable: false,
493 get: function() {
packages/react/src/__tests__/ReactElement-test.js
+20 -2
@@ -313,7 +313,16 @@ describe('ReactElement', () => {
313 expect(React.isValidElement({})).toEqual(false);
314 expect(React.isValidElement('string')).toEqual(false);
315 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);
326 }
327 expect(React.isValidElement(Component)).toEqual(false);
328 expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
@@ -473,7 +482,16 @@ describe('ReactElement', () => {
482 expect(React.isValidElement({})).toEqual(false);
483 expect(React.isValidElement('string')).toEqual(false);
484 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);
495 }
496 expect(React.isValidElement(Component)).toEqual(false);
497 expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
packages/react/src/__tests__/ReactElementJSX-test.internal.js
+20 -2
@@ -69,7 +69,16 @@ describe('ReactElement.jsx', () => {
69 expect(React.isValidElement({})).toEqual(false);
70 expect(React.isValidElement('string')).toEqual(false);
71 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);
82 }
83 expect(React.isValidElement(Component)).toEqual(false);
84 expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
@@ -301,7 +310,16 @@ describe('ReactElement.jsx', () => {
310 expect(React.isValidElement({})).toEqual(false);
311 expect(React.isValidElement('string')).toEqual(false);
312 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);
323 }
324 expect(React.isValidElement(Component)).toEqual(false);
325 expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
packages/react/src/__tests__/ReactElementValidator-test.internal.js
+11 -1
@@ -445,7 +445,17 @@ describe('ReactElementValidator', () => {
445 return <div />;
446 }
447
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 +
459 expect(
460 () => TestFactory.type,
461 ).toWarnDev(