useWatch$()

Use useWatch$() to execute a function before the initial render and whenever the tracking values change. The function executes before rendering, but it can't delay rendering, so if useWatch$() is asynchronous, the rendering will happen before the useWatch$() is fully executed.

Tracking store changes

The useWatch$() hook receives a track() function used for setting up subscriptions that will automatically rerun the useWatch$() hook. The set of track() properties resets on each useWatch$() execution, therefore it is important to always use track() to set up subscriptions anew. Because of this it is possible for the useWatch$() to subscribe to different properties over time.

Cleanup

The useWatch$() hook can return a cleanup callback. This is useful for cleaning up any resources before new execution.

Example

Edit Tutorial