Simple Example:
// A reactive signal from anywhere (global data, props, ...).
const name = reactive("User");
function YourComponent() {
const nameSnapshot = useReactiveSnapshot(() => name.value);
// snapshot is automatically up to date
}
Multiple values, with props:
function YourComponent({firstName, lastName}) {
// You can use complex expressions in your compute function,
// just like in a computed signal.
// React-dependencies used in the callback must be listed in the second argument,
// just like when using `useMemo()` or `useEffect`.
const snapshot = useReactiveSnapshot(() => {
return {
firstName: firstName.value,
lastName: lastName.value,
fullName: `${firstName.value} ${lastName.value}`
};
}, [firstName, lastName]);
// snapshot.firstName, snapshot.lastName, snapshot.fullName are available and up to date
}
Evaluates the
computefunction and returns its result.The body of the compute function is tracked: it will be re-evaluated whenever its reactive dependencies change. The outer react component will re-render whenever
computereturns a new value.