Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactjs: Are there advantages to using React components over normal functions?

Tags:

reactjs

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'))
like image 545
Steve Avatar asked Feb 12 '15 10:02

Steve


People also ask

What are the benefits of using React over traditional Dom?

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.

What is ReactJS tell us about the advantages and disadvantages of using ReactJS?

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.

What is ReactJS What are the features of ReactJS What are the advantages of ReactJS?

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.


1 Answers

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).

like image 181
WiredPrairie Avatar answered Oct 19 '22 08:10

WiredPrairie