For example creating this component in Reactjs as it would be with EcmaScript6?
var HelloWorldComponent = React.createClass({
render: function() {
return <div className="message">Hello World! </div>
}
})
For example creating this component in Reactjs as it would be with EcmaScript6?
var HelloWorldComponent = React.createClass({
render: function() {
return <div className="message">Hello World! </div>
}
})
so it would be
Here you must take into account how the implementation of your component will be.
The example you describe is what is known in react as a presentation component so you can use a class with only one constructor. You don't even have to inherit from
React.Component
This translates to
Which is a syntax supported by react
There is an even simpler syntax which is using arrow functions
There is always the traditional option
Which basically does the same thing as everything above but with more code and harder to read.
In case it is a container component, it is important that you understand that it
React.createClass
does not behave exactly the same asextends React.Component
. In this case you must callsuper(props)
in the constructor and you no longer need to callcomponentWillMount
since the constructor is in charge of this step.In ES5 the function
React.createClass
ensures that the value ofthis
is correct in the methods of your component, however itextends React.Component
does not do the same so you have two optionsmanual binding
Arrow functions
You can read more about the two types of components in Simple and Smart Components and more about what to consider when using React and ES6 .
Another way you have to create a stateless or "stateless" component is the following:
With ES6 you must extend a
Component
. Your example in ES6 is like this: