Taking for example this sample from the React.js docs
var Avatar = React.createClass({
render: function() {
return (
<div>
<ProfilePic username={this.props.username} />
<ProfileLink username={this.props.username} />
</div>
);
}
});
var ProfilePic = React.createClass({
render: function() {
return (
<img src={'http://graph.facebook.com/' + this.props.username + '/picture'} />
);
}
});
var ProfileLink = React.createClass({
render: function() {
return (
<a href={'http://www.facebook.com/' + this.props.username}>
{this.props.username}
</a>
);
}
});
React.render(
<Avatar username="pwh" />,
document.getElementById('example')
);
For cases like this, with components with no state, and if I'm not going to use jsx: are there any disadvantages (performance or otherwise) to just using functions instead of creating components? IE, reducing it to something like this (written in ES6)
var { a, div, img } = React.DOM;
var Avatar = (username) =>
div({}, ProfilePic(username), ProfileLink(username));
var ProfilePic = (username) =>
img({src: `http://graph.facebook.com/${username}/picture`});
var ProfileLink = (username) =>
a({href: `http://www.facebook.com/${username}`}, username);
React.render(Avatar(username), document.getElementById('example'))
The React basically allows developers to utilize individual parts of their application on both client-side and the server-side, which ultimately boosts the speed of the development process. In simple terms, different developers can write individual parts and all changes made won't cause the logic of the application.
React JS has also gained popularity due to the presence of a handy set of tools. These tools make the task of the developers understandable and easier. The React Developer Tools have been designed as Chrome and Firefox dev extension and allow you to inspect the React component hierarchies in the virtual DOM.
It is simple: The component-based approach, automatic rendering, and use of just plain JavaScript make React very simple to learn, build a web (and mobile applications), and support it. We can mix Javascript and HTML together to create a special syntax called JSX which makes it easier to grasp and work with it.
If you are not using JSX, and do not need any of the run-time features of the core classes (like componentDidMount
, etc), then there is no advantage to creating the classes unnecessarily, and in fact, it will be (slightly) more efficient overall.
By creating concrete (yet very simple) wrapper functions such as you've done with Avatar
, it improves code readability. However, if you ever do start using JSX, you'll need to switch to createClass
so that properties are properly passed (since the initial properties are not passed to the constructor function as they are in your code).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With