Skip to content
Bunshi
GitHub

onMount

onMount(fn): void

Registers a lifecyle callback for when a molecule is used (mounted).

This lifecycle will be called everytime this molecule is used in a new scope.

For example, if your molecule is scoped by some UserScope then onMount will be called for “User A” and “User B”.

molecule(()=>{
   let i = 0;
   onMount(()=>{
     const id = setInterval(() => console.log("Ticking...", i++),1000);
     return () => clearInterval(id);
   })
  return i;
})

Parameters

ParameterTypeDescription
fnMountedCallbackA callback to run when a molecule is used

Returns

void

Source

src/vanilla/lifecycle.ts:62