Not clear how to use state API with an object #17
Answered
by
artydev
brandonros
asked this question in
Q&A
-
Sorry if I missed it in docs. <!doctype html>
<html>
<head>
</head>
<body>
<script type="module">
import * as van from 'https://vanjs.org/code/van-0.11.0.js'
const { button, span } = van.tags
class Counter {
constructor() {
this.state = van.state({
amount: 0
})
}
onIncrementButtonClickFactory(self) {
return (event) => {
console.log(event)
self.state.amount += 1
}
}
onDecrementButtonClickFactory(self) {
return (event) => {
console.log(event)
self.state.amount -= 1
}
}
render() {
const incrementButtonOnClick = this.onIncrementButtonClickFactory(this)
const derementButtonOnClick = this.onDecrementButtonClickFactory(this)
const incrementButton = button({ onclick: incrementButtonOnClick }, "👍")
const decrementButton = button({ onclick: derementButtonOnClick }, "👎")
return span(`❤️ ${this.state.amount} `, incrementButton, decrementButton)
}
}
window.addEventListener('load', function () {
const node = document.body
const counter = new Counter()
van.add(node, counter.render())
})
</script>
</body>
</html> I was trying to teach my friend who doesn't know programming how to use a really cool lightweight library like this while showing him basic concepts of HTML/JavaScript/ES6/frontend development. I couldn't discern from the docs how to make this work. |
Beta Was this translation helpful? Give feedback.
Answered by
artydev
May 25, 2023
Replies: 1 comment 8 replies
-
Hey Brandonros, Look at this discussion VanJSIntro Best Regards |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But try to follow Tao advise :-)