Last active
January 22, 2024 08:25
-
-
Save mweststrate/b4ebb27d7c4dcb7e3a37090b91490da7 to your computer and use it in GitHub Desktop.
MobX + webcomponents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://unpkg.com/[email protected]/lib/mobx.umd.js"></script> | |
<script> | |
var MobxDemo = Object.create(HTMLElement.prototype); | |
MobxDemo.attachedCallback = function() { | |
var state = mobx.observable({ | |
counter : parseInt(this.getAttribute("counter")) | |
}) | |
var div = document.createElement("div") | |
function render () { | |
div.innerHTML = "Counter: " + state.counter | |
} | |
div.onclick = function() { | |
state.counter += 1 | |
} | |
var stopAutoRender = mobx.autorun(render) | |
this.detachedCallback = stopAutoRender | |
this.appendChild(div) | |
}; | |
document.registerElement('mobx-demo', { prototype: MobxDemo }); | |
</script> | |
<body> | |
<mobx-demo counter="17" /> | |
</body> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you this is great!