-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inline event handling #12
Comments
An interesting thread, not sure if this is why with my initial testing it didn't work? |
Ok, here's a very raw version of how it could be done inline. Not great to write but useful in the context of #69 👀 class Counter extends HTMLElement {
constructor() {
super();
this.count = 0;
}
connectedCallback() {
this.render();
}
increment() {
this.count += 1;
this.render();
}
decrement() {
this.count -= 1;
this.render();
}
// TODO ideally a patch over a full re-render would be ideal
// update() {
// this.querySelector('span#count').textContent = this.count;
// }
render() {
const { count } = this;
// parentElement can be reverse engineered by walking up from this to wcc-counter
// in this case 2; button -> div -> wcc-counter
this.innerHTML = `
<div>
<button onclick="this.parentElement.parentElement.decrement();"> + </button>
<span>You have clicked <span id="count">${count}</span> times</span>
<button id="inc" onclick="this.parentElement.parentElement.increment();"> + </button>
</div>
`;
}
}
customElements.define('wcc-counter', Counter); Technically the But obviously if we can just patch the dynamic parts a la Lit, then that would be 👌 With Shadow DOM, hoping render() {
const { count } = this;
this.innerHTML = `
<div>
<button onclick="this.shadowRoot.decrement();"> + </button>
<span>You have clicked <span id="count">${count}</span> times</span>
<button onclick="this.shadowRoot.decrement();"> + </button>
</div>
`;
} |
Nothing really to do here, unless we want to support something like this outside of JSX? |
Type of Change
Summary
Want to make sure that this pattern applies (assuming it should) to help ensure declarative event handling, e.g.
Details
The current example looks like this, which is more imperative.
That said, I'm not sure if one is more or less useful for something like #11 ? Because if we want to strip out "dead code", which one is easer to detect via AST?
The text was updated successfully, but these errors were encountered: