useSignalEffect not as precise as useEffect with signals properties #654
-
Hello, Let me explan this with an example from a preact component: import { useEffect } from 'preact/hooks';
import { useSignal, useSignalEffect } from '@preact/signals';
...
const myStateMachine = useSignal({ isWalking: false, isEating: false });
...
useSignalEffect(() => {
console.log(`USE = isWalking changed ${myStateMachine.value.isWalking}`);
});
useSignalEffect(() => {
console.log(`USE = isEating changed ${myStateMachine.value.isEating}`);
});
useEffect(() => {
console.log(`UE = isEating changed ${myStateMachine.value.isEating}`);
}, [myStateMachine.value.isEating]);
useEffect(() => {
console.log(`UE = isWalking changed ${myStateMachine.value.isWalking}`);
}, [myStateMachine.value.isWalking]);
useEffect(() => {
setTimeout(() => {
console.log(`gonna write whole value (change #1)`);
myStateMachine.value = {
...myStateMachine.value,
isWalking: true,
};
}, 2000);
}, []);
useEffect(() => {
setTimeout(() => {
console.log(`gonna write only isWalking (change #2)`);
myStateMachine.value.isWalking = false;
}, 5000);
}, []); Doing so, I noticed that useEffect hook works always as expected and with specific precision, while useSignalEffect does not work as "I would expect".
Commenting these results:
I am asking this question in the process of understanding how and if I can mix signals with standard hooks. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It sounds like you are looking for something like These things can be found described on our pages as well https://preactjs.com/blog/introducing-signals/ |
Beta Was this translation helpful? Give feedback.
It sounds like you are looking for something like
deepsignal
by default signals are shallow reactivity which is the easiest thing to reason about. You change the whole object so it will trigger anything listening for said object. If say, every property on said state machine was a signal and you'd dostateMachine.foo.value
then it would run granularly.These things can be found described on our pages as well https://preactjs.com/blog/introducing-signals/