Combining preact signals and useEffect (preact + preact/compat) #4873
-
Hey there! I’m using I'm also using I have a shared signals class like this: import { signal, computed } from '@preact/signals';
class SharedSignals {
obj1 = signal<ObjType | null>(null);
val1 = signal<string | null>(null);
isSet = computed(() => Boolean(this.obj1.value && this.val1.value));
}
export const shared = new SharedSignals(); Then, in a component, I want to redirect the user when import { useEffect } from 'react';
import { useLocation } from 'wouter-preact';
export function SomeComponent() {
const [location, navigate] = useLocation();
useEffect(() => {
if (shared.isSet.value) {
if (location !== "/") {
navigate("/loc2");
}
}
}, [location, navigate, shared.isSet.value]);
} But the effect doesn't seem to run when Then I tried using The solution that worked for me was converting a signal to a I haven't found anything on that topic in the docs, the examples on the signals are pretty basic, and there is no read on how to use it with existing React libraries that provide different useCallbacks, states, etc. Also, I’m a backend engineer, so I might be missing something obvious on the frontend/reactivity side. I’d love some insight from those more experienced with Signals and Preact, as I do not know all the internals of the frontend frameworks. Thanks in advance! package versions: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Using Unrelated, but you probably don't need |
Beta Was this translation helpful? Give feedback.
Using
signal.value
in auseEffect
callback should work just fine, your issue is likely something else. Any chance you can make an example in our REPL?Unrelated, but you probably don't need
navigate
into your dependency array, it's likely already stable.