Tutorial: Setup once
Tutorial — run one-time setup logic after a component first renders with the effect.once() primitive.
Use effect.once() to run logic once, immediately after the component renders for the first time. Unlike effect(), it does not re-run on state changes — making it the right place for one-time setup such as seeding initial data or initializing third-party libraries.
The callback provides host, signal, and hydrated in its context, so you have direct access to the mounted element and a cancellation signal.
Similar concepts
- React:
useEffectwith an empty dependency array - Vue:
onMounted() - Svelte:
onMount()