サンプル
実践的なサンプル。ライブデモ。
サンプルを選ぶと全画面で開き、ライブプレビューとソースが左右に並んで表示されます。
サンプルを選ぶと全画面で開き、ライブプレビューとソースが左右に並んで表示されます。
名前を編集してみてください。静的なテキストは、要素が組み立てられた時点で固定されています。動的なテキストは update() のたびにもう一度走ります。forceUpdate() ならツリーごと作り直すので、静的なテキストにも変更が届きます。
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) }, }), )