onMount
Use .onMount() 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 work.
The callback receives the full component context, so you have direct access to
state and derived. This makes .onMount() ideal for seeding initial data:
fetch a resource, resolve its result, and write it into state — Ilha will
re-render automatically once the data arrives.
The example uses .onMount() to fetch the Pokémon list once. For the selected
Pokémon's data, it uses .effect() which re-runs whenever state.pokemon() changes.
The fetchPokemon function inside the effect reads the current selection and
fetches the corresponding data, writing the result to state.pokemonData().
Because .onMount() and .effect() don't await async functions directly, each
fetch is wrapped in an inner async function and called immediately — a common
pattern for async work inside synchronous callbacks.
Pokémon and PokéDex are trademarks of Nintendo/Creatures Inc./GAME FREAK inc. This tutorial uses the PokéAPI for educational purposes only and is not affiliated with or endorsed by the Pokémon Company.
Similar concepts
- React:
useEffectwith an empty dependency array - Vue:
onMounted() - Svelte:
onMount()