Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between returning a Javascript expression or a React fragment with a child with the same expression

Is there a difference between

const Component = (props)=>{... return (JS_EXPRESSION)}

and

const Component = (props)=>{... return <>{JS_EXPRESSION}</>}

More specifically, I got a lot of cases where JS_EXPRESSION is a ternary operation
mainly

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

JS_EXPRESSION= condition? <Component1/>: <Component2/>

Is there any difference between the two?
For examples in terms of mounting and unmounting?

>Solution :

No, there are is no difference between those, to quote the official docs:

Grouping elements in Fragment has no effect on the resulting DOM;

and thus also no impact on (un)mounting the component.


You could however just return the ternary operator itself to reduce the code:

const Component = (props)=> condition ? <Component1/> : <Component2/>;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading