Skip to main content

watch

watch({ ...opts })

Executes the specified if/then functions immediately with the current state and subsequently when state accessed changes.

Watch options:

PropertyDescriptionDefault
fromWhat store to retrieve state from.
pathThe state path to retrieve. Specified as a dotted path string, with support for arrays. Sample: "my.field.path[10].name"
ifThe watch query function. Any changes to fields accessed by this function will trigger a re-execution.
thenThe watch mutation then function. This function will be called when the result of if is true. The state may be modified here.
elseThe watch mutation else function. This function will be called when the result of if is false. The state may be modified here.
errorThe watch mutation error function. This function will be called when an exception occurs. The state may be modified here.
strictWhen true, the fields the function accesses will be tracked on every execution instead of on just the first one.true
defaultThe default value when the state is undefined.
throttleMilliseconds to throttle executions
debounceMilliseconds to debounce executions
debugWhen true, log messages regarding state changes will be printed to the console.
verboseWhen true, verbose log messages are printed to the console.
idDebug log uses this to identify components

Example

Live Editor
Result
Loading...

Watch Debugging

import { Watch } from "carry-on-store";

Watch.Debug = true;
Watch.Verbose = true;

You can turn on watch debugging to send debug messages to the console.