Esempi
Esempi concreti. Demo live.
Scegli un esempio e aprilo a schermo intero: anteprima live e sorgente, fianco a fianco.
Scegli un esempio e aprilo a schermo intero: anteprima live e sorgente, fianco a fianco.
Cambia il nome. Il testo statico è stato fissato alla nascita dell'elemento, quello dinamico gira di nuovo a ogni update(). forceUpdate() ricostruisce l'albero, e allora si allinea anche il testo statico.
static: Hello, Alice
dynamic: Hello, Alice
import 'nuclo' let name = 'Alice' let host: Element | undefined const Greeting = () => div( input({ value: () => name, onInput: (e) => { name = e.currentTarget.value; update() }, }), p('static: ', `Hello, ${name}`), // built once p('dynamic: ', () => `Hello, ${name}`), // runs on every update() ) export const App = () => div( div({ onMount: (el) => { host = el as unknown as Element } }, Greeting()), // Calls Greeting() again and patches the same nodes: the static text // catches up. The explicit form rebuilds only this tree. button('forceUpdate()', { onClick: () => { if (host) forceUpdate(Greeting(), host) }, }), )