Skip to content

Show a soft error when a text string or number is supplied as a child to non text wrappers #21953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ import type {
import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils';
import {create, diff} from './ReactNativeAttributePayload';

import invariant from 'shared/invariant';

import {dispatchEvent} from './ReactFabricEventEmitter';

import {
@@ -251,10 +249,11 @@ export function createTextInstance(
hostContext: HostContext,
internalInstanceHandle: Object,
): TextInstance {
invariant(
hostContext.isInAParentText,
'Text strings must be rendered within a <Text> component.',
);
if (__DEV__) {
if (!hostContext.isInAParentText) {
console.error('Text strings must be rendered within a <Text> component.');
}
}

const tag = nextReactTag;
nextReactTag += 2;
10 changes: 5 additions & 5 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
@@ -147,11 +147,11 @@ export function createTextInstance(
hostContext: HostContext,
internalInstanceHandle: Object,
): TextInstance {
invariant(
hostContext.isInAParentText,
'Text strings must be rendered within a <Text> component.',
);

if (__DEV__) {
if (!hostContext.isInAParentText) {
console.error('Text strings must be rendered within a <Text> component.');
}
}
const tag = allocateTag();

UIManager.createView(
Original file line number Diff line number Diff line change
@@ -665,7 +665,7 @@ describe('ReactFabric', () => {
});
});

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

expect(() => {
act(() => {
@@ -694,7 +694,7 @@ describe('ReactFabric', () => {
11,
);
});
}).toThrow('Text strings must be rendered within a <Text> component.');
}).toErrorDev(['Text strings must be rendered within a <Text> component.']);
});

it('should not throw for text inside of an indirect <Text> ancestor', () => {
Original file line number Diff line number Diff line change
@@ -473,7 +473,7 @@ describe('ReactNative', () => {
);
});

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

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

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

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