1

I'm using FlatList as follows:

<FlatList
      data={nodes}
      renderItem={({ item }) => <Goal goal={item} />}
      keyExtractor={item => item.id}
/>

I'm getting this error:

Error: Text strings must be rendered within a component.

when following component is passed to renderItem

const Goal = (props: { goal: GoalProps }) => {
  const listTags = props.goal.tags.map(tag =>
    <li key={tag.id}> {tag.name} </li>
  );
  return <>
    <Text>goal title: {props.goal.title}</Text>
    <ul>{listTags}</ul>
  </>
}

When I simply return just one <Text> then it works without error. This works:

const Goal = (props: { goal: GoalProps }) => {
  const listTags = props.goal.tags.map(tag =>
    <li key={tag.id}> {tag.name} </li>
  );
  return <Text>goal title: {props.goal.title}</Text>
}

How can I pass a more complex component to FlatList? Or maybe there is another list component I can use? I'm new to React and React Native.

Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
Sami
  • 11
  • 2
  • 2
    `li` and `ul` tags are not available in React Native. Are you building them from RN components somewhere else? – Abe Feb 18 '23 at 02:58
  • @Abe, thanks for pointing that out, no I'm simply using those tags. But, I get same error when I return a `<>test text>`. So, anything more than one – Sami Feb 19 '23 at 03:12
  • @Abe, that was it! Thank you! I must have had another error when I used `<>test text>` before, I have made some other changes and now it works if I just remove the `li` and `ul`. I only needed a few `Text` components after each other. First time for me to get an answer on Stackoverflow! Will you be posting your comment as an answer please? – Sami Feb 19 '23 at 12:29
  • Glad it worked. I could, but I'd rather ask you submit it as a solution to this question: https://stackoverflow.com/questions/52368342/invariant-violation-text-strings-must-be-rendered-within-a-text-component. That's a good central place for this error message and your case doesn't appear to be covered there. – Abe Feb 19 '23 at 18:08
  • 1
    Thanks! Done. https://stackoverflow.com/a/75524192/1572322 – Sami Feb 21 '23 at 17:57

0 Answers0