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.

Always call event handlers with component as "this"

See original GitHub issue

Quite new to preact, but one thing struck my immediately. Wouldn’t it be nicer if event handlers were called with the component in question as the thisArg automatically. Otherwise everybody needs to handle it everywhere with e.g. lambda bindings or explicit bind calls. E.g. something like

// Suggested code inside preact
const handler = listener.bind(compInstance);
el.addEventListener('someEvent', handler);

Maybe I’m missing something. Thanks for a nice product!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
developitcommented, Aug 5, 2018

Just a heads up, the value of this in event handlers is likely to change in an upcoming major version of Preact. Closing for now since it’ll likely change to the element the handler was bound to (matching how addEventListener works).

1reaction
developitcommented, Jun 26, 2018

I think we’re going to see some movement in the component definition space soon - perhaps away from ES Classes once again, to simple factories that handle autobinding for you.

Personally, when I can get away with it, I prefer to use a decorator to declaratively bind methods to my component instances:

import { bind } from 'decko';

class Foo extends Component {
  @bind
  handleClick (e) {
    console.log(this);  // our Foo instance!
  }
  render () {
    return <button onClick={this.handleClick}>Click Me</button>
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Always call event handlers with component as "this" #1147
Quite new to preact, but one thing struck my immediately. Wouldn't it be nicer if event handlers were called with the component in...
Read more >
Event handling (overview) - Event reference - MDN Web Docs
Event handler code can be made to run when an event is triggered by assigning it to the target element's corresponding onevent property, ......
Read more >
Handling Events :: Eloquent JavaScript
Though we have ignored it so far, event handler functions are passed an argument: the event object. This object holds additional information about...
Read more >
Event Listeners in React Components - Pluralsight
You can create an event listener in a React app by using the window. ... Now you just need to call removeEventListener when...
Read more >
React Event Handler - Robin Wieruch
In this React tutorial, we will get to know event handlers in React for HTML elements such as button and input elements.
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