Using condition on signal value [React] #616
-
Hello everyone, I'm using Preact signals in my React application, and there's something I don’t fully understand. I'll simplify my example to explain my question. Let's say I have a signal called export const counter = signal<number>(0); I want to display it in my component, so I do something like this: const Test = () => {
return (
<div>
<p>{counter}</p>
</div>
);
}; Here, everything works fine. However, now I only want to display the counter if its value is greater than 0: const Test = () => {
return (
<div>
<p>{counter.value > 0 ? counter : ""}</p>
</div>
);
}; But when I add this check on To update it, I do something like this: counter.value += 1; I feel like I'm missing something about how signals work. I couldn't find an answer in the documentation. Could someone explain how components listen to signal updates? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Are you using the Babel plugin or the |
Beta Was this translation helpful? Give feedback.
It looks like you've misread.
useSignals()
, notuseSignal()
.useSignals()
is an alternative to the Babel plugin that will make a component reactive.useSignal()
is an alternative tosignal()
and used for creating a signal inside of a component.You will need to use
useSignals()
in every component that consumes a signal.