-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Allow $derived to optionally receive callbacks #9984
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
Comments
Checking at compile time (or, as would be required in your last example, at runtime) whether an expression is a function and treating that differently seems like a bad idea to me. What happens when you want your derived value to itself be a function? Wrap it in another function layer? |
Overloads are generally not a good idea, so I am much more in favor of the separation approach (#9968). This would cause unnecessary confusion, especially when people try to use const component = $derived(condition ? ComponentA : ComponentB);
const snippet = $derived(condition ? snippetA : snippetB); They are functions and the incorrect call will throw an exception. |
You're right, I think the solution described in #9968 is more appropriate. |
I don't mind about solution #9968, but I don't really believe that overloading would make things confusing const component = $derived(() => condition ? ComponentA : ComponentB);
const snippet = $derived(() => condition ? snippetA : snippetB); |
It's a bad proposal because it adds a mental load on the developer - you need to think about the type you pass to |
Closing in favor of #9968 |
Describe the problem
Currently $derived only receives values, which maintains a cleaner syntax when using expressions
But when it is necessary to use any minimally more complex logic, it is necessary to get out of your mental model and write much less natural code, extracting the logic into a separate named function (naming is difficult), or creating an IIFE
Describe the proposed solution
Unlike #9250 and #9968 that suggest changes to the api or a new method, I suggest an addition, supporting callbacks using function overloading
This way it would be possible to keep the simplest syntax for expressions and use callbacks only when necessary
Something like:
Not only would this facilitate the use of more complex logic, it would also make it possible to receive reactive values from functions that return callbacks
Alternatives considered
Continue writing code unnaturally by extracting into named functions or IIFEs
Importance
nice to have
The text was updated successfully, but these errors were encountered: