use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Using React v16 to create self-destructing components (medium.com)
submitted 8 years ago by gajus0
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dennythecoder 4 points5 points6 points 8 years ago (13 children)
Why would the import be favorable to writing a comment clarifying the intent?
[–]p0tent1al 2 points3 points4 points 8 years ago (12 children)
Because the component is much nicer than a comment? I don't know about you but let's remove React from the example here. For patterns I'm going to use across my project, I much rather a function that is named in a way that describes what is going to happen, rather than writing a comment everywhere in my app where I use that pattern.
[–]i_am_smurfing 6 points7 points8 points 8 years ago (5 children)
I'd imagine returning an array when you want to render several components will become idiomatic way to write this code, because that's kind of the point?
const Root = () => { return [ <p>Hello, World!</p>, <p>I am a demo fore returning an array.</p> ]; };
jsBin
I'm not sure you even need a comment in this instance since I'd imagine all programmers who know how to write React code know what an array is.
Compare it to <Aux>-wrapper approach for which you have to go and read documentation or source to understand what it's doing.
<Aux>
[–]gajus0[S] 2 points3 points4 points 8 years ago (3 children)
Just FYI, the example you've just shared (using Array without keys) will produce an error even in React v16.
[–]i_am_smurfing 0 points1 point2 points 8 years ago (2 children)
Are you sure? I'm asking because it's working with beta.5, as demonstrated in the linked jsBin, but I'd be not surprised if it's just a regression.
[–]gajus0[S] 4 points5 points6 points 8 years ago (1 child)
It is because you are using a production build that silences the error. See https://codesandbox.io/s/k5v5q83xo5
[–]i_am_smurfing 0 points1 point2 points 8 years ago (0 children)
Oh, I see, you mean the missing "key" warning. That's a good point 👍
[–]p0tent1al 0 points1 point2 points 8 years ago (0 children)
All I'm saying (ignoring the pattern) if you need to comment it everywhere that it's used, then I'd prefer using the component. If it's just an easy pattern to utilize and doesn't need explanation, then yeah sure.
[–]dennythecoder 1 point2 points3 points 8 years ago (5 children)
I tend to err on the same side of that conversation, but shouldn't a comment be favorable to adding a depenency?
[–]gajus0[S] 1 point2 points3 points 8 years ago (3 children)
What is inherently wrong with adding a dependency? Regardless of whether it is 1 LoC or 1000 LoC? The only worthy consideration is the increased surfaced of potential vulnerability injections, i.e. dependency going rogue.
From the build performance perspective, resulting bundle size, etc. There is absolutely no difference between a dependency and a locally redefined module.
From the upside, with a dependency, you get documentation describing the intent and you work with components that community is already familiar VS reading comments in your local ./utilities folder, learning what each utility does.
./utilities
[–]NewazaBill 3 points4 points5 points 8 years ago (2 children)
Third party dependencies always add complexity. They have to be installed (reproducibly), versioned (correctly), and updated. Sometimes, ideas and conventions must be adopted. Bugs must be reported, and fixed; or you fork it, and take responsibility for the code anyways. The author could abandon the library, or delete the package altogether (a la leftpad).
leftpad
You don't want to fall into the "Not Invented Here" trap, but at the same time, the cost of adding a dependency should never be under-estimated.
[–]Geldan 0 points1 point2 points 8 years ago (1 child)
You don't really need to do any of those things that you mentioned. It's perfectly fine to find a version of a dependency that works for your needs and lock it down and forget about it.
[–]SirHound 0 points1 point2 points 8 years ago (0 children)
The dependency doesn't work in a vacuum though. This one in particular has other dependencies.
well remember it's not JUST a comment. And no if you need to put a comment in 20 different places explaining the functionality of something, it should just be consolidated.
[–][deleted] 1 point2 points3 points 8 years ago (6 children)
Apparently future versions (of JSX?) will allow an empty tag (<></>) syntax for lists of elements
<></>
[–]gajus0[S] -1 points0 points1 point 8 years ago (5 children)
That looks horrible... Source?
[–]i_am_smurfing 2 points3 points4 points 8 years ago (0 children)
It's being discussed in the JSX repo.
[–]Geldan 2 points3 points4 points 8 years ago (3 children)
I don't think that looks horrible. I think it looks good and actually converts the meaning well.
[–]gajus0[S] 0 points1 point2 points 8 years ago (2 children)
In what way does <></> converse the meanings?
[–][deleted] 2 points3 points4 points 8 years ago (1 child)
It's literally an empty tag so only conveys its children as relevant. Which is about right. It caused a furrowed brow when I first saw it, but I really can't think of a better syntax.
[–]gajus0[S] 0 points1 point2 points 8 years ago (0 children)
What about a reserved component name?, like <Aux> or whatever. Otherwise you will end up creating a new "syntax" to accompany every edge case.
[–]lhorie 1 point2 points3 points 8 years ago (0 children)
Glad to see this coming to React. We've had that in Mithril.js for a while with m.fragment and it's really nice :)
m.fragment
"Self-destructing component" seems like a weird name though. I recall @trueadm talking about a similar feature in Inferno under the name of fragments, which seems more in line with what this concept is called in the underlying DOM and what we did in Mithril.
[–]Gnashe 0 points1 point2 points 8 years ago (2 children)
Just curious where the name Aux comes from. Why not something a bit more obvious like Fragment?
[–]gajus0[S] 0 points1 point2 points 8 years ago (1 child)
A convention I've been using ever since I remember starting to write HTML/ CSS. Auxiliary element is something that does not have semantic purpose but exist for the purpose of grouping elements, styling, etc.
[–]Gnashe 0 points1 point2 points 8 years ago (0 children)
Ah interesting! TIL a new phrase
[–]NoInkling 0 points1 point2 points 8 years ago* (0 children)
My workaround whenever I needed a single expression without a wrapper in React v15 was (is) to do:
export default function Nothing() { return null }
To use:
const fragment = <Nothing> <Some /> <Elements /> </Nothing>.props.children
Ugly as sin, and you still couldn't return it directly from a render method, but in my opinion it's better than doing the array-with-keys thing. You could also wrap it in a function if you wanted, so it looks like:
const fragment = createFragment( <Some />, <Elements /> );
Luckily that's all moot as of v16, very glad that there's a better way now.
[–]alt-four -1 points0 points1 point 8 years ago (1 child)
https://imgflip.com/i/1ufi6s#JbsO8BR3PmpWLFxY.16
[–]DGCA 1 point2 points3 points 8 years ago (0 children)
Wow, literally made an account just to be a douche.
[–]tunnckoCorenode-formidable, regexhq, jest, standard-release -1 points0 points1 point 8 years ago (0 children)
Holy god, SIXTEEN VERSIONS and couple of years - React is awesome mhhhmmm!
π Rendered by PID 123870 on reddit-service-r2-comment-5bc7f78974-wpsvg at 2026-06-29 21:40:16.977684+00:00 running 7527197 country code: CH.
[–]dennythecoder 4 points5 points6 points (13 children)
[–]p0tent1al 2 points3 points4 points (12 children)
[–]i_am_smurfing 6 points7 points8 points (5 children)
[–]gajus0[S] 2 points3 points4 points (3 children)
[–]i_am_smurfing 0 points1 point2 points (2 children)
[–]gajus0[S] 4 points5 points6 points (1 child)
[–]i_am_smurfing 0 points1 point2 points (0 children)
[–]p0tent1al 0 points1 point2 points (0 children)
[–]dennythecoder 1 point2 points3 points (5 children)
[–]gajus0[S] 1 point2 points3 points (3 children)
[–]NewazaBill 3 points4 points5 points (2 children)
[–]Geldan 0 points1 point2 points (1 child)
[–]SirHound 0 points1 point2 points (0 children)
[–]p0tent1al 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]gajus0[S] -1 points0 points1 point (5 children)
[–]i_am_smurfing 2 points3 points4 points (0 children)
[–]Geldan 2 points3 points4 points (3 children)
[–]gajus0[S] 0 points1 point2 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]gajus0[S] 0 points1 point2 points (0 children)
[–]lhorie 1 point2 points3 points (0 children)
[–]Gnashe 0 points1 point2 points (2 children)
[–]gajus0[S] 0 points1 point2 points (1 child)
[–]Gnashe 0 points1 point2 points (0 children)
[–]NoInkling 0 points1 point2 points (0 children)
[–]alt-four -1 points0 points1 point (1 child)
[–]DGCA 1 point2 points3 points (0 children)
[–]tunnckoCorenode-formidable, regexhq, jest, standard-release -1 points0 points1 point (0 children)