-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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(core): useConstant, createSignal, createComputed$ #6319
Conversation
✅ Deploy Preview for qwik-insights ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Deploying qwik-docs with Cloudflare Pages
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You rock 🚀
@wmertens can we merge this and have the TODO in another PR? |
I merged because I think the API will remain the same even though we'll be able to call it in more places. Here's how to use it dynamically within the component render function. This could also be part of a hook function that forces rerenders. import { component$, useConstant, createSignal, useSignal, useTask$ } from '@builder.io/qwik';
export default component$(() => {
const rerender = useSignal(0)
// force rerender by referencing the signal in the render body
console.log('render', rerender.value)
const sigs = useConstant([])
sigs.push(createSignal(0))
useTask$(({track}) => {
track(rerender)
for (const s of sigs) s.value++
})
return (
<main>
<div>Counts: {sigs.map(s => <span>{s.value}, </span>)}</div>
<p>
<button onClick$={() => rerender.value++}>Click</button>
</p>
</main>
);
}); |
The API looks interesting, but I see a few issues. Firstly, the main issue for me is that it complicates the API and makes it a bit confusing to understand what I should work with. Should I use The use of By the way, in the last example, Additional Confusing Points:
|
@wmertens isn't it marked as deprecated? |
@shairez indeed, but we did invite feedback for this tech preview. @barel-mishal createComputed$ is the counterpart of createSignal. Both should be used when you need to create signals dynamically. useSignal and useComputed$ can only create signals statically.
The reason for that is that
Hope this clears things up? |
This adds the following APIs:
useConstant(value)
: stores a fixed value for the lifetime of the componentcreateSignal(value)
: same as useSignal but can be called a variable amount of times and always returns a new SignalcreateComputed$(cb)
: same as createSignal but for computed signalsNote that the create* API calls still need to run within the component context. This is only available inside the render function and the useConst callback.
Perhaps we should also make the context available inside tasks, as long as we forbid calling hooks (by forbidding useSequentialContext).
Example:
TODO
createSignal
work at any time. Does it really need the container context?createComputed$
not need a Task so it's not bound to an element, or else don't add this yet. Ideally, the qrl is called and cached in the getter, and when one of the used signals changes the cache is cleared and subs triggered.