Beginner problem with eventProxy #3947
-
I am just starting with preact and having issue with event listeners Here is the example
And on click it produces error
Looks like Line 152 in de08e91 Am I missing something obvious ? |
Beta Was this translation helpful? Give feedback.
Answered by
JoviDeCroock
Mar 24, 2023
Replies: 1 comment 1 reply
-
In your example you are passing a string to onClick rather than an actual reference, in backtick land we need to use import { h, Component, render } from 'https://esm.sh/preact@10.13.1';
import htm from 'https://esm.sh/v112/htm@3.1.1/es2022/htm.mjs';
const html = htm.bind(h);
class App extends Component {
onPlay(ev) {
ev.preventDefault();
console.log('HERE');
}
render() {
return html`<button onClick=${this.onPlay.bind(this)}>Click</button>`;
}
}
render(html`<${App} />`, document.body); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JoviDeCroock
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your example you are passing a string to onClick rather than an actual reference, in backtick land we need to use
${}
to pass a JS value. Note the added$
here right afteronClick=
, I also addedev
to the onPlay arguments else it would crash.updated Fiddle