Skip to content

Commit

Permalink
docs(tutorial): Lifecycle Hooks - useWatch task (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmon committed Nov 20, 2022
1 parent 24ad44f commit 8812c6e
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export default component$(() => {
debouncedValue: '',
});
useWatch$(({ track }) => {
// rerun this function when `value` property changes.
track(() => store.value);
// Set up timeout for debounced value.
const id = setTimeout(() => (store.debouncedValue = store.value), 500);
// return cleanup function in case `value` property changes before time is up.
return () => clearTimeout(id);
// Use track to rerun this function when store's `value` property changes.

// Setup a timer to copy `value => debouncedValue` after half a second.

// Return cleanup function in case `value` property changes before time is up.
return () => {
// cleanup code
};
});
return (
<>
Expand Down

0 comments on commit 8812c6e

Please sign in to comment.