question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event handlers have no context?

See original GitHub issue

Looks 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Jan 3, 2018

this

1reaction
mindplay-dkcommented, Jan 3, 2018

Ugh, yeah, I see.

Man, why does the fat arrow operator even produce a Function instance? Neither bind() nor call() actually works like it does on other Function instances - those methods simply don’t work. JS FTW 🙄

Oh well. It’s not possible, so I suppose we can close this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found