Rook source
comp Counter() {
state count = 0
return <main>
<button onClick={count++}>{count}</button>
<p>{count * 2}</p>
<small>{"always here"}</small>
</main>
}
document.body.append(<Counter />)
Nothing found.
Change a value and follow the pulse through its dependency graph. Only the DOM nodes that read it update; the independent label stays still.
Ready · count = 0
comp Counter() {
state count = 0
return <main>
<button onClick={count++}>{count}</button>
<p>{count * 2}</p>
<small>{"always here"}</small>
</main>
}
document.body.append(<Counter />)
updated nodes flash
0
always heremount { ... } in a component's body is code that needs the markup on the page: starting a timer, listening to window events, setting the focus, measuring an element, loading data.
mount runs for every instance of the component: with two <Fruit /> in a list, it runs twice. And it runs only when the markup is in the document. That is checked after all the synchronous code that created the component, in a microtask: by then document.body.append(...)or an {if} branch has usually inserted the markup, and the browser has not drawn a frame yet, so focus() and measuring sizes work. Markup created earlier and inserted later, say in a timer, mounts when it is inserted.
// mount runs for every instance of a component, after all the synchronous code of the // program, when the markup is already on the page comp Fruit(name string) { const item = <li>{name}</li> mount { console.log("mount:", name, "on the page:", item.isConnected) } return item } console.log("creating the list") document.body.append(<ul> <Fruit name="Guava" /> <Fruit name="Lychee" /> </ul>) console.log("the list is inserted")
What mount defers with defer, as an effect does, runs when {if}, {switch}, {for} or a ?: branch in markup removes the component: that is where you stop what mount started. Press "Run", then hide the clock: the timer stops.
// mount runs once the component's markup is on the page. What it defers runs when // {if} removes the component, so the timer does not keep running for nothing comp Clock() { state time = new Date().toLocaleTimeString() mount { const timer = setInterval(() => { time = new Date().toLocaleTimeString() }, 1000) defer clearInterval(timer) } return <p class="card">It is {time}</p> } comp App() { state shown = true return <div> <button onClick={shown = !shown}>{shown ? "Hide the clock" : "Show the clock"}</button> {if shown { <Clock /> }} </div> } document.body.append(<App />)
When the board connects to a live task feed, its component starts listening inmount and removes that listener in defer. Hiding the feed panel therefore stops work instead of leaving a stale subscription behind.
mount is a function that runs later, like an event handler: changes of state in it update the markup, and return returns from it, not from the component.mount returns nothing. defer f(x) takes x at once, and defer { ... } reads the values when it runs: that is how to clean up what comes later, e.g. defer { editor?.dispose() }.mount is written at the top level of a component's body; there can be several, and they run in order.{if}, {switch} or {for} removes the markup before it gets into the document, mount does not run.element.remove()): its cleanup does not run.mount in the component that creates the list. Code needed once when the program starts goes simply after document.body.append.