"Text strings must be rendered within a Text component" when && operator is used in JSX
See original GitHub issue@hramos here. This issue has been modified from its original content, in order to convey the latest status of this issue. You may refer to the edit history to see what @mattermoran originally reported.
Environment:
[skip envinfo]
This issue only affected Android in older releases, but is now consistent across both iOS and Android platforms as of 0.57.8
.
Description
Using JSX that conditionally renders a Text
element can lead to a RedBox error, “Invariant Violation: Text strings must be rendered within a Text component”, if the condition is “falsy”, as in the following example:
<View>
{someTextValue && <Text>{someTextValue}</Text>}
</View>
This can lead to a confusing development experience, as the pattern is documented in React’s JSX docs.
Workaround
Use a ternary operator.
<View>
{someTextValue ? <Text>{someTextValue}</Text> : null}
</View>
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:24 (4 by maintainers)
Top Results From Across the Web
Invariant Violation: Text strings must be rendered within a ...
Invariant Violation: Text strings must be rendered within a <Text> component. 99% of cases will be caused by using conditional rendering ...
Read more >Text Strings must be rendered within a <Text> component
Invariant Violation: Text strings must be rendered within a component. This error is located at: in RCTView (at View.js:45) in View (at header....
Read more >How to handle the error “Text strings must be rendered within ...
The first cause for the error is because of bad indentation. It is very necessary that each component is indented properly. The child...
Read more >Text strings must be rendered within a <Text> component ...
Empty string scenario: Let's say one of your variables you're trying to use for conditional rendering evaluates to an empty state. Guess what ......
Read more >Text strings must be rendered within a Text component
Hello learners,In this video I have explained why you get this error( Text strings must be rendered within a Text component ) and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone that wants to see the errors in person Will crash
Will not crash
I know whats going wrong,
The below code works without crashing in iOS whereas it crashes in Android. I checked it snack.expo v29.0.0.
Leaving aside the crash, you should not render a empty string without wrapping it in Text for now in React-Native. In these case you must do,
By the way still this is a bug in RN, to maintain compatability either it should crash in both iOS and Android or it should emit omit empty string(‘’) altogether.