4

Both assignment are different. Is there any difference between both of them or both are equal?

First

onClick(){
  return (
     <div></div>
  )
}

Second

const onClick= ()=> {
   return(
       <div></div>
   )
 }
HAMMAD ALI
  • 143
  • 2
  • 13
  • 2
    Does this answer your question? [Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?](https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable) – Robin Zigmond Jun 14 '20 at 15:51
  • My question is somehow related to this but not the all. What is the difference in rendering from a arrow function and a function? – HAMMAD ALI Jun 14 '20 at 15:56
  • There is a typo in your first code `onClick(){` should be `function onClick(){` – isAif Jun 14 '20 at 15:58
  • 1
    No, it's identical. You might only be interested in what happens when the function returns JSX (which is compiled to regular Javascript objects anyway), but the differences between doing so in an arrow function or "regular" function are still exactly those defined by the language specification. Which is exactly what's covered in the duplicates. In short: in the toy example you give, there is no difference. But there may be if you use `this` inside the function, or in other rare cases. – Robin Zigmond Jun 14 '20 at 15:58
  • Does this answer your question? [Function or fat arrow for a React functional component?](https://stackoverflow.com/questions/54331084/function-or-fat-arrow-for-a-react-functional-component) – Kerem atam Jun 14 '20 at 15:59
  • i think when we add function to the statement then it will be equivalent to the second type. – HAMMAD ALI Jun 14 '20 at 16:01

3 Answers3

5

ES5 Functions

This type of function doesn't bind with the current context you need to bind it manually, if we do not bind the function with this then the function will find its binding by itself while executing the function according to the execution context.

this.onClick = this.onClick.bind(this);
onClick() {
  return <div></div>;
}

ES6 Functions

Simply known as arrow functions, in arrow function there is no binding of this so no effect of this keyword according to the execution context

onClick = () => {
  return <div></div>;
}

How Execution Context Effect?

Es5 function behave differently when executed if the function is bounded with any other instance(object) then the this refer to that instance if there is no binding then the this keyword refers to its parent scope.

The setTimeout executed in the global execution context the callback function es5Function which is not bound to any instance, the this keyword in the function will refer to its parent scope which is in our case window

<script>
  window.name = "Global Scope";

  function App() {
    this.name = "App Scope";

    let es5Function = function () {
      console.log("I am Es5 function I am in the " + this.name);
    };

    let es5FunctionWithBinding = function () {

      console.log(
        "I am Es5 function binded with THIS, I am in the " + this.name
      );

    }.bind(this);

    let es6Function = () => {
      console.log("I am Es6 function I am in the " + this.name);
    };

    return [es5Function, es5FunctionWithBinding, es6Function];
  }

  const [es5Function, es5FunctionWithBinding, es6Function] = new App();

  setTimeout(es5Function, 1000);
  setTimeout(es5FunctionWithBinding, 1000);
  setTimeout(es6Function, 1000);

</script>

Using as Function Constructor

One more simple difference is we can not use arrow functions as a function constructor

The question is why?

When we create an object using function constructor (with new keyword) then the function returns the this keyword by default which refers to a new memory location every time.

So simple conclusion

  • Es5 function has it's own this when using it with new keyword so it returns it by default
  • Es6 arrow function throw an error [function name] is not a constructor ( because arrow function doesn’t has its own this)

<script>
  function Es5(name) {
    this.name = name;
    // by default it will return this when using with `new` keyword
  }

  const Es6 = (name) => {
    this.name = name; // here this refers to global scope which is window
  };

  let es5Instance = new Es5("MUHAMMAD ILYAS");
  console.log("Es5 instance => Name:", es5Instance.name);

  try {
    let es6Instance = new Es6("jsfit");
  } catch (e) {
    console.log("Es6 with new keyword => ERROR:", e.message);
  }

  let es6InstanceWithoutNewKeyword = Es6("jsfit");
  // es6InstanceWithoutNewKeyword will be Undefined because nothing was returned from Es6 function
  console.log("ES6 without new keyword: ", es6InstanceWithoutNewKeyword);

  // I am in the global scope so here `this` refers to `window` so both will result the `name` exactly the same

  console.log("Global scope: ", this.name, "or", window.name);
</script>
MUHAMMAD ILYAS
  • 1,402
  • 7
  • 23
  • But you dont always need to bound context, very rarely... If you're going to have a lot of instances of this component on your page - better use normal function as it will not be created for each instrance - it will remain in prototype. Arrow functions will be created for each instance which will unnecesserily consume memory – Dmitry Reutov Jun 14 '20 at 16:19
3

What is the difference in rendering from a arrow function and a function?

There is no difference at all.

Arrow functions differ from regular functions in how they handle this. There are other differences as well but difference related to handling of this is the most significant one.

Not related to your question but you should know that first onClick function cannot be used inside functional components unless function keyword is written before the function name and second onClick function cannot be used inside class components unless const keyword is removed.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
-1

Difference is You can change the elements of a constant but you cannot reassign the constant

Prashanthi
  • 145
  • 5