Vadorequest
Vadorequest

Reputation: 17997

Why React uses functions as variables?

I'm getting started with React and I'm wondering why they choose to use this notation:

const counter = (state, action) => {}

instead of the old-traditional:

function counter(state, action){}

I'm wondering if that's because of the const keyword. Because it is not possible to create a const function as it (as stated there: Are there constants in JavaScript?)

is that the only reason? I understand that const functions are important in React, to ensure the behavior isn't changed at runtime. But I wonder if that's the only reasons why they choose to use it.

Upvotes: 3

Views: 98

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

It is a personal choice, it is new ES2015 feature called arrow function,

const counter = (state, action) => { }

// you can also define counter like this
const counter = function (state, action) { }

The main difference is that arrow function does not have its own this and arguments

Upvotes: 2

Related Questions