1

I would like some advice as to the best practice when conditionally rendering an element or component in react-native. My question is when the conditional is not true is it better to return null or just run the if condition? I understand that if you return null then the lifecycle methods are still run but my concern is if i do not return anything is there an impact or performance difference?

Example One

renderText(name) {
  if(name === 'Abba') {
    return <Text>{name}</Text>
  }
}

Example Two

renderText(name) {
  if(name === 'Abba') {
    return <Text>{name}</Text>
  } else {
    return null
  }
}
1
  • 1
    For the topmost element it is a good practice to either return some JSX or null, i.e return name === 'Abba' ? <Text>{name}</Text> : null. If it is inside of some JSX, you can use the && operator instead, i.e name === 'Abba' && <Text>{name}</Text>. Commented Aug 6, 2018 at 3:26

1 Answer 1

1

I think conditional rendering like this might be better.

renderText(name) {
  return (
    { name === 'Abby' &&
      <Text>
        {name}
      </Text>
    }
  )
}
Sign up to request clarification or add additional context in comments.

Comments

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.