-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(signals-preact): useLiveSignal #367
Conversation
🦋 Changeset detectedLatest commit: 525c50e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
❌ Deploy Preview for preact-signals-demo failed.
|
Size Change: +298 B (0%) Total Size: 69.2 kB
ℹ️ View Unchanged
|
Oohh interesting. I hadn't thought of this kind of use case before. Thanks for bringing this up! One thought about this new hook is that it requires the child component ( I wonder if there are other patterns we could instead encourage to handle the scenario mentioned in the Readme. It appears there are two signals that hold a value for which there is another "current" value. Could this "current" value be a signal, i.e. Though having to change state twice (once for React state and another for signal state) isn't great. It'd be nice to just do I'd be curious if there are other situations where switching signal instances in rendering may be relevant cuz perhaps I need to dwell on it a bit more to better understand it use cases. Anyways, that's a lot of thoughts. Would love to hear what you think! |
Why we cannot do it like that? import { useComputed, useLiveSignal, useSignal } from '@preact/signals';
// The signal representing props.date can change
// when i.e. someone presses a butotn
function Todo({ date }) {
const formattedDate = useComputed(() => formatDate(liveDateSignal.value));
return <span>{formattedDate}</span>;
}
function App() {
const dateA = useSignal(0);
const dateB = useSignal(1);
const on = useSignal(true)
return (
<main>
<button onClick={() => {on.value = !on.peek()}}>
Toggle
</button>
<Todo date={useComputed(() => on.value ? dateA.value : dateB.value)} />
</main>
);
} |
I think in most cases when we are writing component powered by signals we should stick with then and think about component like about |
@andrewiggins I see your point, I honestly dislike the Or we could just opt to drop this entirely and bet more on education for signals, as I guess this fundamentally could be considered education something along the lines of, if the reference to a signal can change you have to use a |
@JoviDeCroock maybe it can be solved with eslint plugin for signals that will enforce stable reference to signals |
Resolve #366
This adds
useLiveSignal
as a way to wrap a signal passed into a component which can change reference.