Closed mindplay-dk closed 1 year ago
We don't provide a version of effect()
bound to component lifecycle yet, but are considering adding it in preactjs/signals#91. Once added, we'll document it.
In the meantime, you can use normal useEffect, it will just trigger a component render:
const count = useSignal(0);
// run every time count.value changes:
useEffect(() => {
console.log(count.value);
}, [count.value]);
Alternatively, you can use the implementation we're likely to land - it's pretty short:
import { effect } from '@preact/signals';
// runs on mount, when any signal deps change, and cleans up on unmount:
function useSignalEffect(callback) {
useEffect(() => effect(callback), []);
}
useSignalEffect(() => {
console.log(count.value);
});
useSignalEffect
has landed, so I think this is good to close out.
The "Local state with signals" section covers only
useSignal
anduseComputed
- it doesn't cover effects triggered by local signals.Can we use normal
useEffect
for that?Would you specify
deps
as e.g.signal
orsignal.value
?Or is this a part of the Signals story that has yet to unfold? 🙂