Fun fact: take a hook that uses a subscription (e.g. by directly using useSyncExternalStore), and use it as a component...
You now have a signal, written in React, with 0 external dependencies.
functionuseFoo(){returnuseSyncExternalStore(/* ... */);}functionNormal(){constfoo=useFoo();returnfoo==='bar'?BarComponent:OtherComponent;}// Temporary uppercase name to make JSX work.constFooSignal=useFoo;// Yes, this only needs to happen once for the whole app.const$foo=<FooSignal/>;functionWithSignals(){return<div>
// ...
<h2>{$foo}</h2><p> Some other stuff that last rendered at {performance.now()}</p></div>;}
This works with any argumentless hook that properly manages global state.
Fun fact: take a hook that uses a subscription (e.g. by directly using
useSyncExternalStore), and use it as a component...You now have a signal, written in React, with 0 external dependencies.
This works with any argumentless hook that properly manages global state.
working codesandbox