1

I just started to learn ReactJS and done some tutorials. Have noticed that some write function and others do not. Some examples below. What is the difference? What should I use and when?

Render

With function

var $class$ = React.createClass({
 render: function() {
   return (
     <div />
   );
 }
});

Without function

const $class$ = React.createClass({
  render() {
    return (
      <div />
    );
  }
});

Update

With function

componentDidUpdate: function(prevProps, prevState) {
  $END$
},

Without function

componentDidUpdate(prevProps, prevState) {
  $END$
},

Default Props

With function

getDefaultProps: function() {
  return {
    $END$
  };
},

Without function

getDefaultProps() {
  return {
    $END$
  };
},

1 Answer 1

3

Those without the function keyword are the result of using the new shorter ES6 method definitions.

You can read more here: Method Definitions - JavaScript | MDN

As far as I am aware, there is no notable difference in behaviour between a shorthand definition and including the function keyword other than the former having reduced support across environments.

Sign up to request clarification or add additional context in comments.

2 Comments

I think with Babel transform, the support for both methods should be same.
@AkshayArora Of course, but potentially using babel doesn't satisfy every possible environment setup so it's still noteworthy :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.