-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
onstate / oncreate is not idiomatic #1765
Comments
I have definitely written hundreds of these |
In many cases, If they did only run on subsequent changes, you'd have to jump through some extra hoops (extracting that code out into a method and calling it from both So while it would make things easier in some cases to change the semantics of Anyway, there's an easy trick you can use that completely sidesteps the issue: instead of using the export default {
oncreate() {
this.on('state', ({ changed, current, previous }) => {
// this code will only run on subsequent updates
});
this.on('update', ({ changed, current, previous }) => {
// this code will only run on subsequent updates
});
}
}; |
Thank you for the tip ! As you can see, I'm not saying that Although, I'm saying that unless you like to have big chunks of code for different topics in your Thank you. |
Perhaps this is more of a documentation issue then? Given the symmetry between |
Maybe a docs problem yes. Actuallay I don't see any difference between |
See https://svelte.technology/guide#lifecycle-hooks - it's a matter of whether the DOM has already been updated for that state. That could probably be made more prominent than comments in a block of code. |
Yes it seems it is the only documented difference at this point. Kind of disappointing to have to check for Thank you |
Hi,
I assume that many developers use a very common pattern : reacting to changes. It seems odd that both
onstate
andonupdate
fire once with the initial properties of a component.I think it would be way more expected that
onupdate
fires only when an actual update of the component is made. The current implementation seems more like anaftercreate
but event there I find thechanged
property meaningless. Or I am missing something useful there.Having to check for
if(previous)
in eachonupdate
implementation seems cumbersome.Thank you.
The text was updated successfully, but these errors were encountered: