Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active January 22, 2024 08:25
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mweststrate/b4ebb27d7c4dcb7e3a37090b91490da7 to your computer and use it in GitHub Desktop.
Save mweststrate/b4ebb27d7c4dcb7e3a37090b91490da7 to your computer and use it in GitHub Desktop.
MobX + webcomponents
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/mobx@2.6.0/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>
@alexandraweaver
Copy link

Thank you this is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment