Event handlers have no context?
See original GitHub issueLooks like event-handlers have no this
context?
For example:
<button onclick={ () => console.log(this) }>Hello</button>
It looks like this
is the number 0
?
I was expected maybe this
would reference the event target - the button
element?
(If we address this, it likely needs to be addressed separately for the life-cycle events.)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:13 (12 by maintainers)
Top Results From Across the Web
How does JavaScript assign context to this of event handlers?
Event handlers are invoked in the context of a particular object (the current event target) and are provided with the event object itself....
Read more >Scope in the Context of Event Handlers | by Claire DeBoer
One final approach to try is using state by setting a global variable that you can then pass in and out of functions...
Read more >Event Handlers for DOM Event Listeners Lose the Context of ...
If you add a DOM event listener, the "this" keyword loses its context in the event handler when the event is raised:.
Read more >Context of Event Handlers | The React Workshop
The reason is that because the event handler code is essentially wrapping up the call and calling our event handler function for us,...
Read more >Handling Events :: Eloquent JavaScript
Event listeners are called only when the event happens in the context of the object they are registered on. That example attaches a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
thisUgh, yeah, I see.
Man, why does the fat arrow operator even produce a
Function
instance? Neitherbind()
norcall()
actually works like it does on otherFunction
instances - those methods simply don’t work. JS FTW 🙄Oh well. It’s not possible, so I suppose we can close this issue?