Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does (state = {}) => state means

I was building an App in react where I found a line in one of the boiler plate projects.

(state = {}) => state

Can anyone explain to me what the above line means? It's javascript ES6 standard.

like image 251
Shivanand Mitakari Avatar asked Feb 20 '16 17:02

Shivanand Mitakari


People also ask

What does state mean in JavaScript?

A state is a JavaScript object that stores the dynamic data of a component and allows it to follow changes between renderings. It's a combination of all those different states. For instance, if the user is not authenticated, is the modal open.

What is the difference between state and setState in React js?

State can be updated in response to event handlers, server responses, or prop changes. This is done using the setState() method. The setState() method enqueues all of the updates made to the component state and instructs React to re-render the component and its children with the updated state.


2 Answers

That's an arrow function with a default parameter that returns its input or an empty object, if no input was provided. It is similar to this es-5 function:

function(){
    var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
    return state;
}
like image 58
baao Avatar answered Oct 08 '22 17:10

baao


It is a(n arrow) function that returns its input. If the input is not defined it will become the default value {}.

You might have seen it in combination with using redux' "connect", as the function that maps the store's state to the projection used for the connected component. If there is no state available, the empty object will be provided.

like image 28
flq Avatar answered Oct 08 '22 19:10

flq