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.
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.
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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With