Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TUTORIAL: Lifecycle Hooks - useWatch task #1160

Merged
merged 2 commits into from
Nov 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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