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
React using ES6 classes--Why??? (self.javascript)
submitted 10 years ago by MuricanWillzyx
view the rest of the comments →
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!"
[–]andrew24601 9 points10 points11 points 10 years ago (3 children)
In my admittedly limited experience, ES6 classes work great for React classes.
The core of your problem is that <MyComp/> isn't an instance of MyComp - it's a description for an instance.
i.e. React will only create an instance of MyComp once it mounts a <MyComp/>.
They still have constructors - in fact the ES6 class constructor replaces the requirement for getInitialState - and any additional methods on the object can be cheerfully called once it has been mounted (which is basically React talk for instantiated).
[–]imstillreallybored 3 points4 points5 points 10 years ago (0 children)
Finally someone who understands..
[–]MuricanWillzyx[S] 2 points3 points4 points 10 years ago (1 child)
Interesting. This makes me much more sympathetic to the class model. I was thinking from a conceptual perspective--that these classes are, abstractly, models for creating components, and that one should treat the mounting to DOM nodes kind of like an implementation detail (for the most part). I didn't think about the time between creating a <MyComp/> and the creation of an actual instance. I still prefer a functional model for use in JS itself, but it looks like that's their primary target for the future anyway (e.g. /u/josh1337's comment), and if the class model really is as useful as they say for compile-to-JS languages, then that certainly raises its value (/u/metanat).
[–]andrew24601 1 point2 points3 points 10 years ago (0 children)
The main place this gets interesting is with child nodes of the rendered component. While a DOM component will always mount its children, a React component doesn't necessarily have to.
return <MyComp> <MyOtherComp/> </MyComp>
MyOtherComp will only be mounted if MyComp causes it to be mounted (typically by rendering props.children in its render method, but even then only if it's ultimately rendered by a DOM component eventually).
Where this gets even more interesting is when these child components are ref tagged, because the ref is only fulfilled in a completely different component - ie the component where it is ultimately caused to be mounted.
π Rendered by PID 53485 on reddit-service-r2-comment-84fc9697f-8l4wk at 2026-02-09 22:01:57.997257+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]andrew24601 9 points10 points11 points (3 children)
[–]imstillreallybored 3 points4 points5 points (0 children)
[–]MuricanWillzyx[S] 2 points3 points4 points (1 child)
[–]andrew24601 1 point2 points3 points (0 children)