Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9b2028

Browse files
authoredAug 10, 2021
Show a soft error when a text string or number is supplied as a child to non text wrappers (#21953)
* Show soft errors when a text string or number is supplied as a child instead of throwing an error * bring __DEV__ check first so that things inside get removed in prod. * fix lint
1 parent da627de commit e9b2028

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed
 

‎packages/react-native-renderer/src/ReactFabricHostConfig.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import type {
2222
import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils';
2323
import {create, diff} from './ReactNativeAttributePayload';
2424

25-
import invariant from 'shared/invariant';
26-
2725
import {dispatchEvent} from './ReactFabricEventEmitter';
2826

2927
import {
@@ -264,10 +262,11 @@ export function createTextInstance(
264262
hostContext: HostContext,
265263
internalInstanceHandle: Object,
266264
): TextInstance {
267-
invariant(
268-
hostContext.isInAParentText,
269-
'Text strings must be rendered within a <Text> component.',
270-
);
265+
if (__DEV__) {
266+
if (!hostContext.isInAParentText) {
267+
console.error('Text strings must be rendered within a <Text> component.');
268+
}
269+
}
271270

272271
const tag = nextReactTag;
273272
nextReactTag += 2;

‎packages/react-native-renderer/src/ReactNativeHostConfig.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ export function createTextInstance(
147147
hostContext: HostContext,
148148
internalInstanceHandle: Object,
149149
): TextInstance {
150-
invariant(
151-
hostContext.isInAParentText,
152-
'Text strings must be rendered within a <Text> component.',
153-
);
154-
150+
if (__DEV__) {
151+
if (!hostContext.isInAParentText) {
152+
console.error('Text strings must be rendered within a <Text> component.');
153+
}
154+
}
155155
const tag = allocateTag();
156156

157157
UIManager.createView(

‎packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ describe('ReactFabric', () => {
524524
});
525525
});
526526

527-
it('should throw for text not inside of a <Text> ancestor', () => {
527+
it('should console error for text not inside of a <Text> ancestor', () => {
528528
const ScrollView = createReactNativeComponentClass('RCTScrollView', () => ({
529529
validAttributes: {},
530530
uiViewClassName: 'RCTScrollView',
@@ -542,7 +542,7 @@ describe('ReactFabric', () => {
542542
act(() => {
543543
ReactFabric.render(<View>this should warn</View>, 11);
544544
});
545-
}).toThrow('Text strings must be rendered within a <Text> component.');
545+
}).toErrorDev(['Text strings must be rendered within a <Text> component.']);
546546

547547
expect(() => {
548548
act(() => {
@@ -553,7 +553,7 @@ describe('ReactFabric', () => {
553553
11,
554554
);
555555
});
556-
}).toThrow('Text strings must be rendered within a <Text> component.');
556+
}).toErrorDev(['Text strings must be rendered within a <Text> component.']);
557557
});
558558

559559
it('should not throw for text inside of an indirect <Text> ancestor', () => {

‎packages/react-native-renderer/src/__tests__/ReactNativeMount-test.internal.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ describe('ReactNative', () => {
473473
);
474474
});
475475

476-
it('should throw for text not inside of a <Text> ancestor', () => {
476+
it('should console error for text not inside of a <Text> ancestor', () => {
477477
const ScrollView = createReactNativeComponentClass('RCTScrollView', () => ({
478478
validAttributes: {},
479479
uiViewClassName: 'RCTScrollView',
@@ -487,9 +487,9 @@ describe('ReactNative', () => {
487487
uiViewClassName: 'RCTView',
488488
}));
489489

490-
expect(() => ReactNative.render(<View>this should warn</View>, 11)).toThrow(
491-
'Text strings must be rendered within a <Text> component.',
492-
);
490+
expect(() =>
491+
ReactNative.render(<View>this should warn</View>, 11),
492+
).toErrorDev(['Text strings must be rendered within a <Text> component.']);
493493

494494
expect(() =>
495495
ReactNative.render(
@@ -498,7 +498,7 @@ describe('ReactNative', () => {
498498
</Text>,
499499
11,
500500
),
501-
).toThrow('Text strings must be rendered within a <Text> component.');
501+
).toErrorDev(['Text strings must be rendered within a <Text> component.']);
502502
});
503503

504504
it('should not throw for text inside of an indirect <Text> ancestor', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.