Signalとは?


A signal is a way to store the state of your application, similar to useState() in React. But there are some key differences that give Signals the edge.
useState
に似た状態を保持する方法だけど、異なる点がいくつかある。

The key difference between Signals and State is that Signals return a getter and a setter, whereas non-reactive systems return a value (and a setter).
明確な違いとして↓がある。
-
useSignal
: getterとsetterを返す -
useState
: 値とsetterを返す
ここで言及しているuseState
はReactのそれで、useSignal
はqwikのものと考えて良いかな。
特定のライブラリ(フレームワーク)に限った話ではなくて、より一般的なものとして考えても大きな相違はないかも。

The issue is that the word State conflates two separate concepts.
「State」には2つの概念が混同している問題がある。
- 状態に対する参照
- 状態の値そのもの

React useState() returns a state-value. This means that useState() has no idea how the state-value is used inside the component or the application. The implication is that once you notify React of state change through a call to setCount(), React has no idea which part of the page has changed and therefore must re-render the whole component. This is computationally expensive.
ReactのuseState()
は状態の値を返し、コンポーネント内部でどのように状態の値が使われるかを知る術がない。なので、全体的に再レンダリングする必要性がある。

To be reactive, Signals must collect who is interested in the Signal’s value. They gain this information by observing in what context the state-getter is invoked. By retrieving the value from the getter, you are telling the signal that this location is interested in the value. If the value changes, this location needs to be re-evaluated. In other words, invoking the getter creates a subscription.
signalだと、getterを通して値を取得することで値を扱っている場所が伝えられる。

React has useRef(), which is similar to useSignal(), but it does not cause the UI to re-render.
useRef
がuseSignal
に似ているけど、これは再レンダリングしない。

Signals rarely require memoization because they do the least amount of work out of the box.
signalにおいてはメモ化の必要性があまりない。