{
const main = () => {
document.querySelectorAll(".Counter").forEach(element => {
render({element: element, state: {value: 0}})
})
}
const render = ({element, state}) => {
console.debug(`Rendering`, state, `into`, element)
let old_root = element.querySelector("#root")
if (!old_root) {
old_root = document.createElement('div')
element.appendChild(old_root)
}
const new_root = document.createElement('div')
new_root.id = "root"
const {value} = state
new_root.innerHTML = `
<span id="current_value">${value}</span>
<button id="increment">Increment</button>
<button id="reset">Reset</button>`
new_root.querySelector('#increment').onclick =
() => update_state({value: value+1})
new_root.querySelector('#reset').onclick =
() => update_state({value: 0})
const update_state = update_object => {
console.debug(`Updating`, state, `with a partial state`, update_object,
`and rerendering (after yielding execution).`)
setTimeout(render({element: element, state: {...state, ...update_object}}))
}
old_root.parentElement.replaceChild(new_root, old_root)
}
main()
page
return "Read this!"
}