DEV Community

React Fragments

Sarah Chima on March 13, 2019

Part of the JSX syntax is that adjacent JSX elements are wrapped in an enclosing tag. That is, a JSX expression must have only one outermost elemen...
Collapse
 
misterhtmlcss profile image
Roger K. • Edited

Hi Sarah,
Great post. I never thought of doing an Input component before, so that was also a great addition skill beyond the Fragment explanation too.
Also I think if you look you see two things, one I believe is an error and the second is what I believe to be a best practice for usability.

  1. Your second last example uses props not this, yet it's a Class declaration. What's tongue-in-cheek cute about the oversight is that you caught it on your next example and used this.

  2. I believe Labels should enclose the input tag for usability reasons. Which if I'm right then it means that extra

    isn't necessary.

    Regardless even if I'm right about any of those this was as I've said an awesome and easy to follow Fragment article. I've read a few as I try to process and learn to develop better with React and yours is good.

    Have a great week!

Collapse
 
sarah_chima profile image
Sarah Chima

Thanks Roger, I'm glad you like the post. Thanks also for pointing out the error. I used the input field solely for an explanation. Should use a better example next time.

Collapse
 
misterhtmlcss profile image
Roger K.

Oh my no it was great. I'm just in the early stages of learning React so for me I loved your Input component and I was serious about replicating it in some form in my own work. I recognize you it's just an 'example', but does that also mean making each input into a component as you've done isn't a good idea?

Btw I tried to use your lesson yesterday and couldn't quite get it to work. Darn! I was so excited to try Fragments finally.

Again thank you for your article. Don't feel bad about your examples, they were very helpful, so in that context they were the perfect choices :)

Collapse
 
daveskull81 profile image
dAVE Inden

Great post. Previously I had only seen the React.Fragment or <> </> methods for using fragments. That’s cool to see the destructuring method to use instead. I like that as it semantically helps to know what the element is doing and helping build tools is great too.

Collapse
 
miku86 profile image
miku86

That's not destructuring, it is a named import.

Differences: youtube.com/watch?v=4keUSxNw-KE

Collapse
 
sarah_chima profile image
Sarah Chima

Thanks Dave. I'm glad you find the post useful.

Collapse
 
daveskull81 profile image
dAVE Inden

I just used the destructuring approach in a Gatsby project of mine. When I first implemented it the code didn't compile, so I came back to this post and found my mistake. I had messed up the import statement. Thanks again!

Thread Thread
 
sarah_chima profile image
Sarah Chima

We all make mistakes as devs. I'm glad the post helped you to fix it.

Thread Thread
 
daveskull81 profile image
dAVE Inden

Oh yes. I’m more than happy to make mistakes and learn from them.

Collapse
 
chiangs profile image
Stephen Chiang

I wonder if there is a performance difference between the two ways of using it. If there was, probably not significant... But if you use React.Fragment in a component where the logic is to conditionally render some jsx or null, would it act like a dynamic import?

Collapse
 
ammunov11 profile image
Amrutha KK

Hi sarah,
Great post!,it's really helpful to understand in detail about fragment.Now i got the perfect definition about fragment,expecting more post like this

Thank you

Collapse
 
sarah_chima profile image
Sarah Chima

Thank you for reading it.

Collapse
 
dance2die profile image
Sung M. Kim

Thanks Sarah.

I liked how the Fragment can be used and what problem it solves πŸ˜„

Collapse
 
sarah_chima profile image
Sarah Chima

I'm glad you like it. Thank you.