I cannot understand why in the arrow functions we do not need to wrap the literal of arrow function in the ({})
braces, instead of in this example the literal just wrapped in the single ()
braces. Why? I had surfed the internet to find an answer on it, but it failed.
And also why we put the arguments in double braces ({})
, instead of just ()
?
const FilterLink = ({ filter, children }) => ( <NavLink to={filter === 'SHOW_ALL' ? '/' : `/${ filter }`} activeStyle={ { textDecoration: 'none', color: 'black' }} > {children} </NavLink> )
Unlike regular functions, arrow functions do not have their own this . The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.
Arrow functions introduce concise body syntax, or implicit return. This allows the omission of the curly brackets and the return keyword. Implicit return is useful for creating succinct one-line operations in map , filter , and other common array methods.
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
Without curly braces {} . With this syntax, the arrow function has an implicit return. For example, the below arrow function returns 42, even though there's no return .
Using ({})
is to destructure
the arguments and => ()
is an implicit return equivalent to => { return ()}
and (
only serves to disambiguate between the start of an object and the opening braces of a function body and would generally be used when you have a multiline return value. You could simply avoid using (
and have the NavLink
in the same line as the arrow =>
const FilterLink = ({ filter, children }) => ( // <-- implicit return <NavLink to={filter === 'SHOW_ALL' ? '/' : `/${ filter }`} activeStyle={ { textDecoration: 'none', color: 'black' }} > {children} </NavLink> )
is equivalent to
const FilterLink = ({ filter, children }) => { return ( <NavLink to={filter === 'SHOW_ALL' ? '/' : `/${ filter }`} activeStyle={ { textDecoration: 'none', color: 'black' }} > {children} </NavLink> ) }
Check this answer for more details on the usage of destructuring in ({ filter, children })
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