Skip to content

"Text strings must be rendered within a Text component" when && operator is used in JSX #20764

Closed
@mattermoran

Description

@mattermoran

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDXIssues concerning how the developer experience can be improved.Platform: AndroidAndroid applications.Platform: iOSiOS applications.StaleThere has been a lack of activity on this issue and it may be closed soon.📮Known IssuesThis indicates an issue that refers to a bug or limitation of RN that is not currently being handled

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions