Skip to content

[feature] watchOnce #530

Closed
Closed
@Razi91

Description

@Razi91

Sometimes it's good to have a watch that will react on change only once. I've made a function for this:

export const watchOnce = <T>(
  target: WatchTarget<T>,
  cb: (res: T, prev?: T) => any
) => {
  const unwatch = watch(target, (res, prev?: T) => {
    unwatch();
    cb(res, prev);
  });
  return unwatch;
}

It returns a function to disable the watch manually.

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions