tldraw / signia

Reactive signals that scale, by tldraw.
https://signia.tldraw.dev
MIT License
903 stars 15 forks source link

Why is `useValue` implemented as it is? #88

Open smoothbricker opened 1 day ago

smoothbricker commented 1 day ago

Hi there,

I'm working on a Event + State management library, building on top of Signia and trying to understand the internals better.

Somehow I was reading the docs but skipped over useValue and tried to implement re-rendering of signals without the library user having to use track().

So I wrote this:

export const useSignal = <S extends Signal<any, any>>(name: string, signal: S) => {
  const [state, setState] = useState(signal.value);
  useEffect(() => react(name, () => setState(signal.value)), []);
  return state;
};

What's wrong with this? It seems to work just fine in a small demo app, but useValue seems to do a lot more things... Would like to know why :-)