question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"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:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:24 (4 by maintainers)

github_iconTop GitHub Comments

18reactions
jimji1005commented, Feb 27, 2019

For anyone that wants to see the errors in person Will crash

{0 && <Text>0</Text>}
{'' && <Text>isEmptyString</Text>}

Will not crash

{undefined && <Text>isUndefined</Text>}
{true && <Text>isTrue</Text>}
{false && <Text>isFalse</Text>}
{1 && <Text>isNumber</Text>}
{null && <Text>isNull</Text>}
{'string' && <Text>isString</Text>}

{!undefined && <Text>!isUndefined</Text>}
{!true && <Text>!isTrue</Text>}
{!false && <Text>!isFalse</Text>}
{!0 && <Text>!0</Text>}
{!1 && <Text>!isNumber</Text>}
{!null && <Text>!isNull</Text>}
{!'string' && <Text>!isString</Text>}
{!'' && <Text>!isEmptyString</Text>}
14reactions
ravirajn22commented, Aug 21, 2018

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.

<View style={styles.container}>
        {'' && <Text style={styles.paragraph}>I am here</Text>}
</View>

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,

<View style={styles.container}>
        // Change any variable to boolean using double exclamation !!
        {!!'' && <Text style={styles.paragraph}>I am here</Text>}
</View>

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found