Skip to content

Commit 1a41a19

Browse files
authored
Append text string to <Text> error message (#19581)
* Append text string to <Text> error message * Truncate text in <Text> error message * Regenerate `codes.json`
1 parent 1287670 commit 1a41a19

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ export function createTextInstance(
244244
): TextInstance {
245245
invariant(
246246
hostContext.isInAParentText,
247-
'Text strings must be rendered within a <Text> component.',
247+
'Text string must be rendered within a <Text> component.\n\nText: %s',
248+
text.length > 100 ? text.substr(0, 88) + ' (truncated)' : text,
248249
);
249250

250251
const tag = nextReactTag;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ export function createTextInstance(
146146
): TextInstance {
147147
invariant(
148148
hostContext.isInAParentText,
149-
'Text strings must be rendered within a <Text> component.',
149+
'Text string must be rendered within a <Text> component.\n\nText: %s',
150+
text.length > 100 ? text.substr(0, 88) + ' (truncated)' : text,
150151
);
151152

152153
const tag = allocateTag();

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,15 @@ describe('ReactFabric', () => {
563563
}));
564564

565565
expect(() => ReactFabric.render(<View>this should warn</View>, 11)).toThrow(
566-
'Text strings must be rendered within a <Text> component.',
566+
'Text string must be rendered within a <Text> component.\n\nText: this should warn',
567+
);
568+
569+
expect(() =>
570+
ReactFabric.render(<View>{'x'.repeat(200)}</View>, 11),
571+
).toThrow(
572+
`Text string must be rendered within a <Text> component.\n\nText: ${'x'.repeat(
573+
88,
574+
)} (truncated)`,
567575
);
568576

569577
expect(() =>
@@ -573,7 +581,9 @@ describe('ReactFabric', () => {
573581
</Text>,
574582
11,
575583
),
576-
).toThrow('Text strings must be rendered within a <Text> component.');
584+
).toThrow(
585+
'Text string must be rendered within a <Text> component.\n\nText: hi hello hi',
586+
);
577587
});
578588

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

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,15 @@ describe('ReactNative', () => {
423423
}));
424424

425425
expect(() => ReactNative.render(<View>this should warn</View>, 11)).toThrow(
426-
'Text strings must be rendered within a <Text> component.',
426+
'Text string must be rendered within a <Text> component.\n\nText: this should warn',
427+
);
428+
429+
expect(() =>
430+
ReactNative.render(<View>{'x'.repeat(200)}</View>, 11),
431+
).toThrow(
432+
`Text string must be rendered within a <Text> component.\n\nText: ${'x'.repeat(
433+
88,
434+
)} (truncated)`,
427435
);
428436

429437
expect(() =>
@@ -433,7 +441,9 @@ describe('ReactNative', () => {
433441
</Text>,
434442
11,
435443
),
436-
).toThrow('Text strings must be rendered within a <Text> component.');
444+
).toThrow(
445+
'Text string must be rendered within a <Text> component.\n\nText: hi hello hi',
446+
);
437447
});
438448

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

scripts/error-codes/codes.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@
254254
"253": "work.commit(): Cannot commit while already rendering. This likely means you attempted to commit from inside a lifecycle method.",
255255
"254": "Element ref was specified as a string (%s) but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a functional component\n2. You may be adding a ref to a component that was not created inside a component's render method\n3. You have multiple copies of React loaded\nSee https://reactjs.org/link/refs-must-have-owner for more information.",
256256
"255": "Expected ReactFbErrorUtils.invokeGuardedCallback to be a function.",
257-
"256": "Expected ReactFiberErrorDialog.showErrorDialog to be a function.",
258257
"257": "Portals are not currently supported by the server renderer. Render them conditionally so that they only appear on the client render.",
259258
"258": "Unknown element-like object type: %s. This is likely a bug in React. Please file an issue.",
260259
"259": "The experimental Call and Return types are not currently supported by the server renderer.",
@@ -270,7 +269,6 @@
270269
"269": "Profiler must specify an \"id\" string and \"onRender\" function as props",
271270
"270": "The current renderer does not support persistence. This error is likely caused by a bug in React. Please file an issue.",
272271
"271": "Failed to replay rendering after an error. This is likely caused by a bug in React. Please file an issue with a reproducing case to help us find it.",
273-
"272": "The current renderer does not support hydration. This error is likely caused by a bug in React. Please file an issue.",
274272
"273": "Nesting of <View> within <Text> is not currently supported.",
275273
"274": "Text strings must be rendered within a <Text> component.",
276274
"275": "The current renderer does not support mutation. This error is likely caused by a bug in React. Please file an issue.",
@@ -361,5 +359,6 @@
361359
"367": "ReactDOM.createEventHandle: setListener called on an element target that is not managed by React. Ensure React rendered the DOM element.",
362360
"368": "ReactDOM.createEventHandle: setListener called on an invalid target. Provide a valid EventTarget or an element managed by React.",
363361
"369": "ReactDOM.createEventHandle: setter called on an invalid target. Provide a valid EventTarget or an element managed by React.",
364-
"370": "ReactDOM.createEventHandle: setter called with an invalid callback. The callback must be a function."
362+
"370": "ReactDOM.createEventHandle: setter called with an invalid callback. The callback must be a function.",
363+
"371": "Text string must be rendered within a <Text> component.\n\nText: %s"
365364
}

0 commit comments

Comments
 (0)