Best practices for conditional rendering in react-native

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Best practices for conditional rendering in react-native



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






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>.
– Tholle
Aug 6 at 3:26



null


return name === 'Abba' ? <Text>name</Text> : null


&&


name === 'Abba' && <Text>name</Text>




1 Answer
1



I think conditional rendering like this might be better.


renderText(name)
return (
name === 'Abby' &&
<Text>
name
</Text>

)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered