constExampleComponent=()=>{constclickHandler=()=>{console.log('I was clicked')}return<buttononClick={clickHandler()}>Clickme</button>
}
Nope! Nothing will get logged.
"I was clicked" will get logged each time the component is rendered, but nothing will be logged when the button is clicked. If a function is called, its side effects will still happen, even if it's called in an unintended way.
The event handler was expecting a function that it can call. However, it got "I was clicked" instead! Not exactly helpful.
onClick received undefined, not the string "I was clicked", because clickHandler doesn't return anything (implicitly returns undefined):
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
The point of the first piece is that nothing will get logged when the button is clicked because we’re talking about the behavior of the click handler. Discussing what happens when the component renders is outside the scope of the conversation.
The second example is a bit confusing. I wanted to match what happens if you execute that same thing in node and what response you see so that people can run it themselves, but it’s not a 1:1 mapping.
Yeah you'll get the logging output, but that's not the same as the return value. The return value is always undefined unless a) an explicit return statement is used to return something other than undefined, or b) it's a single-statement arrow function with no enclosing block:
In the case of console.log, even if you return console.log('...'), the return value will still be undefined, because console.log itself returns undefined.
Sorry if I'm stating the obvious or my explanation is confusing.
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice article! Couple of nitpicks, though:
"I was clicked" will get logged each time the component is rendered, but nothing will be logged when the button is clicked. If a function is called, its side effects will still happen, even if it's called in an unintended way.
onClick
receivedundefined
, not the string "I was clicked", becauseclickHandler
doesn't return anything (implicitly returnsundefined
):The point of the first piece is that nothing will get logged when the button is clicked because we’re talking about the behavior of the click handler. Discussing what happens when the component renders is outside the scope of the conversation.
The second example is a bit confusing. I wanted to match what happens if you execute that same thing in node and what response you see so that people can run it themselves, but it’s not a 1:1 mapping.
Yeah you'll get the logging output, but that's not the same as the return value. The return value is always
undefined
unless a) an explicitreturn
statement is used to return something other than undefined, or b) it's a single-statement arrow function with no enclosing block:In the case of
console.log
, even if youreturn console.log('...')
, the return value will still beundefined
, becauseconsole.log
itself returnsundefined
.Sorry if I'm stating the obvious or my explanation is confusing.