-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ES6 Classes to create React components. #613
Comments
Yes! High on my personal wishlist :-).
|
Just a heads up about some of the concerns regarding ES6 class support and how can can address them:
|
FWIW I've created a simple proof of concept using Traceur to transpile from ES6 class syntax to ES5: https://github.com/bjoerge/react-es6-class/ Passing source code with ES6 class syntax through React.transform seems to work pretty well (at least in this rather limited example), probably due to Esprima's experimental support for ES6. |
👍 since this will help work with other compile-to-javascript languages and frameworks such as typescript and F# (FunScript and WebSharper). |
👍 |
2 similar comments
+1 |
+1 |
One way to handle non-method properties of classes ( You then might end up w/ something like: import {ReactComponent} from "react/ReactComponent";
import {WithMixins, PropValidate} from "react/util";
import props from "react/props";
import MyMixin from "app/components/MyMixin";
var props = {
myProp: props.fn.isRequired
};
@WithMixins(MyMixin)
@PropValidate(props)
class Component extends ReactComponent {
// ...
} Of course, these won't actually be in ES6 (but are on the table for ES7), so you'd be introducing a required build step for this syntax (whether with Traceur's version or a sweetjs macro, etc). Devs who would like to opt-out of that step could either stick to class Component extends ReactComponent {
// ...
}
Component.addMixins(MyMixin);
Component.propValidate(props); |
👍 |
👍 people can be trusted with
class ReactComponent{
componentWillMount(){
console.log('base implementation');
}
}
class Typeahead extends ReactComponent{
componentWillMount(){
super.componentWillMount();
// Additional implementation
}
} or perhaps I am missing something. |
see #1380 I'll close out this issue since most of the info here is stale. |
Please don't use ES6 class or class inheritance at all, anywhere. Specifically in the context of React, you'll be completely missing the point of reactive programming. Super is a code-smell anywhere, but it's particularly abhorrent in reactive programming.
|
I agree with @ericelliott, declare my components with classes feels so weird, using React.createClass feels sugar, the internals are abstracted in this function. Maybe i'm missing something but are there good benefits for use ES6 classes in React? At least i hope you dont deprecate createClass in the future |
I agree that OO concepts such as |
Export a factory instead of a class (like React does today), and you'll help users avoid a jungle full of pitfalls and gorillas. |
@ericelliott Note that classic React factories created through ES6 classes in React is not adding anything you couldn't already do. In fact it is constraining it further by encouraging object composition instead of mixins. It is an unfortunate marketing effect that this move is seen as encouraging OOP when it is really not. My stance on progress in this space is that you can't take things away from developers until you've taught them the alternatives... at scale. (That includes myself.) The class system provides an optional escape hatch when you need it rather than completely stopping you. The primary feature that our class system provides is an "instance" handle this has several features.
If a developer can't figure out a way to do it, we don't want them to get stuck. Therefore, OOP is an escape hatch. At the same time we're trying to teach and encourage composition of components and higher order functions/components instead of OOP. You can still implement that on top of the class systems that we have. Then, when this practice is common enough, we can start deprecating old class systems. However, we make that progress by teaching and encouragement - not by force. As a phase two of this, we can start introducing more pure models. See the alternatives that we've been working on to replace "instances" as an abstraction model: https://github.com/reactjs/react-future/blob/master/01%20-%20Core/03%20-%20Stateless%20Functions.js As well as a declarative ways of updating state: https://github.com/reactjs/react-future/tree/master/09%20-%20Reduce%20State |
The stateless function example you linked is exactly how I want to write components! Ideally with PureRenderMixing behavior built in. Looking forward to that future. |
@sebmarkbage I'm a little hazy on why you need RE: provide inheritance options, see Prototypal Inheritance with Stamps. There's currently a project underway to make stamps produced by Stampit immutable, as well. Re: pitfalls -- In my experience, single inheritance has many more pitfalls than |
RE: Stateless functions - 👍 |
Hi, Is prop validation needed for stateless components. Or it's meaningless(and wrong) to put prop validation as these stateless components are merely the functions and for concept sake called " react's stateless component" ? |
It's never required for any components, but unless you're already using Flow, we suggest you to put const MenuItem = ({ title }) => <div>{title}</div>;
MenuItem.propTypes = {
title: React.PropTypes.string
}; |
@gaearon Thanks. I was wondering that these stateless functions(components) looks simply like functions so how come React treats them as components!! And also, am I correct in saying that these stateless functions don't have life cycle methods like DidComponentMount, etc as they don't extend 'React.Component'. Because presence/absence of state can't be the only difference between usual components and stateless function components. |
Starting from 0.14, React allows components to be declared as functions. This is useful for simple components that have no lifecycle methods or state, and in the future React might apply certain optimizations to them (but not today). Please read the announcement from 0.14 release notes. |
@gaearon Thanks. Now I know that jsx files having simple functions are taken as components by React. |
Well reading above comments I don't know how to tell this. I know you're just against using oop in react applications. Lets forget about oop and go with (quoting @jordwalke) "ES6 classes simply as a better syntax for React component creation". Just to make the syntax better is it possible to support inheritance? I think it'd be much cleaner syntax than wrappers and mixins. |
This is the kind of thinking you get when you use |
I can't believe that somebody actually written that nonsense and I can't believe that it's being referenced. If he can't use/doesn't know how to use oop paradigms doesn't mean he should write something titled "Goodbye, Object Oriented Programming". The kind of thinking you mentioned is the kind of thinking that has led to the browser you're using, operating system you're using, and more than one third of all software has written so far. That kind of thinking which is "to reuse" has its roots in modernism and even the car you drive, the hardware you run your software on, the airplane you travel with, the phone you have in your pocket are using it. Good for the human being that it has reached to a level that |
This thread is not about the validity or success of OOP. React classes already support inheritance if you want it, we just don't recommend it after our experience with it. There's nothing technical to address here so I'd recommend taking the philosophical discussion to Medium, Twitter or a nice Facebook group. |
I'm sorry that discussion happened here. It'd be great if you support inheritance, currently it works but as you know there are some issues with it. Is it possible to plan to solve these issues and support inheritance officially if it's not taking too much effort? It'd result in much cleaner code if nothing else. |
@sebmarkbage My apologies. I'm hopeful that we move toward more functional components in the future so that lots of React users don't have to relearn inheritance pitfalls the hard way. For those who like classes and want to make the best use of them with React, @gaearon wrote an excellent guide, "How to Use Classes and Sleep at Night". Perhaps it would be useful to mention inheritance and some of Dan's recommendations in the documentation to help guide people down paths toward success (HOC) and help them avoid some pitfalls? |
Let's use ES6 classes to create React component classes. We've accumulated some custom concepts that don't lend themselves to using ES6 classes but we can still use them in conjunction with React components as ES6 classes, if the separation is performed as "enhancers" on top of completely pure ES6 classes:
ReactComponent
even though the base class of our components today is calledReactCompositeComponent
. We just need to renameReactCompositeComponent
toReactComponent
andReactComponent
toReactComponentBase
.React.createClass
doesn't return the constructor. It returns the "convenience constructor" which can be invoked withoutnew
. We can unify the two concepts and eliminate convenience constructors altogether.componentWillMount
,componentWillReceiveProps
- there aren't classical OO equivalents.ReactCompositeComponent
will attempt to intelligently merge their results. It is okay to factor all of that out into helper utilities, and we can supply a code mod that automatically updates your code that uses mixins.The text was updated successfully, but these errors were encountered: