0

I want to save the content in a variable depending on the result of an if statement. But when I add multiple lines it doesn't work.

let content = null
if(this.props.group.name != null){
  content = <Text>Just this line works</Text>
              <Text>This doesn't work</Text>
}

I can't find out what to do. I can't add + at the end of the line like in Javascript.

2
  • Can't you just add it next to each other? Like this - content = <Text>Just this line works</Text> <Text>This doesn't work</Text> Commented Nov 10, 2016 at 13:11
  • Not when it is more content. This is just an example. Commented Nov 10, 2016 at 13:12

1 Answer 1

2

Components need to be wrapped in a parent containing component, unless you create it as an array with keys.

// this would work because it's wrapped inside parentheses and has a parent component
content = (
          <View>
            <Text>Just this line works</Text>
            <Text>This doesn't work</Text>
          </View>
          )

// this works because the components are an array
content = [
           <Text key="1">Just this line works</Text>,
           <Text key="2">This doesn't work</Text>
          ]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that worked! I tried the parentheses but I didn't know you also needed to wrap it.
@SinanSamet glad I could help!

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.