Skip to content

Conversation

@seefeldb
Copy link
Contributor

@seefeldb seefeldb commented Jan 9, 2025

  const clickHandler = handler(...);
  ...
  <button onclick={clickHandler({ item }))>

is a bit confusing as it looks like it's called when an event happens, but really we're creating an event handler ahead of timing and binding data to it.

This PR adds a .with method to handlers, so that we can write

  <button onclick=`{clickHandler.with({ item })}`>

which hopefully is a bit clearer.

This PR also overrides .bind to do the same, and then calls the handler with the state parameter bound to this. This requires passing an anonymous function instead of an arrow function:

    const incHandler = handler<
      { amount: number },
      { counter: { value: number } }
    >(function ({ amount }) {
      this.counter.value += amount;
    });

or just

    const incHandler = handler(function (event) {
      this.counter.value += event.amount;
    });

to make typescript happy we need to declare the type of `this`. this requires switching to `function` instead of `=>` and adding a first parameter.

this makes me wonder whether we should just swap the order for handler instead and use `self` or `state` there.
…quires `function` when using `this`, but it's now easier to write.
@anotherjesse anotherjesse merged commit 41b835e into main Jan 9, 2025
@anotherjesse anotherjesse deleted the with-on-handlers branch January 9, 2025 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants