1

I have a nested array of "credits"

const credits = [
  {
    id: 1,
    heading: 'Test Organisation',
    credits: [
      {id: 1, text: 'Joe Bloggs'},
      {id: 2, text: 'Jane Bloggs'},
    ]
  },
....
];

I am wanting to print each "header" in a <Text> element, and then subsequently each of their "credits" in a <Text> element.

I have looked at the following already answered questions on how to achieve this, but one is for ReactJS and uses HTML and the other that uses React Native outputs only the opening tag of a view:

I am being told there is a syntax error as it is expecting a close parentheses after the end of the first <Text> element.

Here is what I have:

render() {
    return (
        <ScrollView style={styles.container}>
          <View style={styles.centredHighlightHeaderWrapper}>
            <Text style={styles.centredHighlightHeader}>
              Credits
            </Text>
          </View>


          {
            credits.map(item => (
                  <Text style={styles.centredHeader}>{item.heading}</Text>#Unexpected token -- js error says expecting "," - phpstorm says expecting ")"

              {
                item.credits.map(credit => (
                    <Text style={styles.name}>{credit.text}</Text>
                ))
              }
            ))
          }

        </ScrollView>
    );
  }

1 Answer 1

2
{
        credits.map(item => (
             <React.Fragment>
               <Text style={styles.centredHeader}>{item.heading}</Text>

               {
                item.credits.map(credit => (
                    <Text style={styles.name}>{credit.text}</Text>
                ))
                }
             </React.Fragment>
        ))
      }

You can't have adjacent react elements without a container. So I've put them both in a React.Fragment for you

Sign up to request clarification or add additional context in comments.

2 Comments

I just came across the fragment in another element so nearly answered my own Q. You beat me to it, so you get the points :D
You could also do <> ... </> instead of <React.Fragment> ... </React.Fragment>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.