Skip to content

Commit 26992a1

Browse files
committed
feat: support string not in Text error
1 parent 56715a9 commit 26992a1

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

src/__tests__/__snapshots__/render-debug.test.tsx.snap

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ exports[`debug changing component: bananaFresh button message should now be "fre
8888
<Text
8989
testID="bananaFresh"
9090
>
91-
not fresh
91+
fresh
9292
</Text>
9393
<TextInput
9494
placeholder="Add custom freshness"
@@ -367,12 +367,6 @@ exports[`debug: another custom message 1`] = `
367367
</View>"
368368
`;
369369

370-
exports[`debug: shallow 1`] = `
371-
"another custom message
372-
373-
"
374-
`;
375-
376370
exports[`debug: with message 1`] = `
377371
"my custom message
378372

src/__tests__/render-string-validation.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ test('should not throw for texts nested in fragments', () => {
7878
).not.toThrow();
7979
});
8080

81-
test('should not throw if option validateRenderedString is false', () => {
82-
expect(() => render(<View>hello</View>)).not.toThrow();
83-
});
81+
// test('should not throw if option validateRenderedString is false', () => {
82+
// expect(() => render(<View>hello</View>)).not.toThrow();
83+
// });
8484

8585
test(`should throw when one of the children is a text and the parent is not a Text component`, () => {
8686
expect(() =>

src/renderer/reconciler.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type TextInstance = {
3434
};
3535

3636
type HostContext = {
37+
elementType: string;
3738
isInsideText: boolean;
3839
};
3940

@@ -130,7 +131,9 @@ const hostConfig = {
130131
_internalHandle: OpaqueHandle,
131132
): TextInstance {
132133
if (!hostContext.isInsideText) {
133-
throw new Error(`Text string "${text}" must be rendered inside <Text> component`);
134+
throw new Error(
135+
`Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "${text}" string within a <${hostContext.elementType}> component.`,
136+
);
134137
}
135138

136139
return {
@@ -207,7 +210,7 @@ const hostConfig = {
207210
* This method happens **in the render phase**. Do not mutate the tree from it.
208211
*/
209212
getRootHostContext(_rootContainer: Container): HostContext | null {
210-
return { isInsideText: false };
213+
return { elementType: 'ROOT', isInsideText: false };
211214
},
212215

213216
/**
@@ -224,14 +227,8 @@ const hostConfig = {
224227
type: Type,
225228
_rootContainer: Container,
226229
): HostContext {
227-
const previousIsInsideText = parentHostContext.isInsideText;
228230
const isInsideText = type === 'Text';
229-
230-
if (previousIsInsideText === isInsideText) {
231-
return parentHostContext;
232-
}
233-
234-
return { isInsideText };
231+
return { elementType: type, isInsideText };
235232
},
236233

237234
/**

src/renderer/renderer.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ test('throws when rendering string outside of Text', () => {
2020
jest.spyOn(console, 'error').mockImplementation(() => {});
2121

2222
expect(() => render(<View>Hello</View>)).toThrowErrorMatchingInlineSnapshot(
23-
`"Text string "Hello" must be rendered inside <Text> component"`,
23+
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <View> component."`,
2424
);
2525

2626
expect(() => render(<Passthrough>Hello</Passthrough>)).toThrowErrorMatchingInlineSnapshot(
27-
`"Text string "Hello" must be rendered inside <Text> component"`,
27+
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <ROOT> component."`,
2828
);
2929

3030
expect(() => render(<>Hello</>)).toThrowErrorMatchingInlineSnapshot(
31-
`"Text string "Hello" must be rendered inside <Text> component"`,
31+
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <ROOT> component."`,
3232
);
3333

3434
jest.restoreAllMocks();

0 commit comments

Comments
 (0)