-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
Default props #5673
Comments
This has come up before in #4442 and there were practical implementation reasons to leave it how it was - and the decision was to document it rather than change it. https://svelte.dev/docs#1_export_creates_a_component_prop now explicitly mentions that this is an initial value rather than a default one, and that changing it to undefined does not revert it to its initial value. As I'm writing this though, I see now that in https://svelte.dev/tutorial/default-values we're still using the word 'default', so that should be changed as well, and I'm labeling this as a documentation issue. |
From my point of view it would be highly confusing if this would be a fallback, not initial value. So I also prefer how it's currently implemented. |
-- Edited this comment, check the bottom paragraph -- @Conduitry I understand what you are saying, and still, there is a fundamental issue with this behavior. I will show you my point with typescript. In the example Svelte compiler tells me that I can render As you can see in the example, after 5 seconds, it will display Don't you agree? <script lang="ts">
//Comp.svelte
export let myProp: number = 5;
</script>
<div>{myProp / 3}</div> <script>
// App.svelte
let bool = true;
setTimeout(() => {bool = false}, 5000);
</script>
{#if bool}
<Comp myProp={1} />
{:else}
<Comp />
{/if} @Conduitry I checked it again. I was wrong, the case I presented above would actually work as I expect. So In this case, I guess I fully understand and this is truly not a bug. I do think there should be a way then to have a default value behavior. Its very basic.. |
You can achieve a default value like this <script>
export let myProp;
$: myProp = myProp ?? 5;
</script> |
@dummdidumm Yes thats what Im doing today. I just wanted something more. Thank you @dummdidumm @Conduitry |
@Conduitry @dummdidumm Closing, but I still want to state that I think Svelte should have a way to declare a default value, and not just an initial value. It doesn't have to be a breaking change, it can be introduced as a new syntax, Im sure a lot of people would use it. I know I would. |
@Conduitry @dummdidumm export let status: ButtonStatus = "primary"; the actual type is: var status: ButtonStatus | undefined and not var status: ButtonStatus which is weird to me because it differs from the TypeScript / JavaScript language implementation. Also this solution I don't think is a good idea since it introduces unnecessary bloat which Svelte normally tries to prevent: <script>
export let myProp;
$: myProp = myProp ?? 5;
</script> There is a similar behavior being fixed in the next TypeScript version (4.4) for interfaces. Does that maybe have something to do with it? microsoft/TypeScript#13195 |
This is an important and dangerous thing! I use Svelte since 2020, but default prop are not, what i expected all the time! It is a bit dangerous, because i thought, that Svelte will use always default value for prop, if value is not set - so I never check props for undefined (nearly all my props have a default value) !!! Problematic specially with arrays/objects
best workaround is to do this: export let items; $: items || [] As you can see - this looks ugly Svelte could do this automagically, you could add option flag or something |
Describe the bug
When declaring a prop,
export myProp
, and adding a default value to it,export myProp = 5
.If a component passes a value and later on changes the value to be
undefined
,myProp
becomes undefined instead of becoming the default value, 5 in this case.Attached a simple repl to display the issue in detail.
https://svelte.dev/repl/861015c678f04ade8b8a5eb8c96c36b9?version=3.29.7
Expected behavior
I believe this should be considered as a bug, since Im declaring that the prop has a default value.
Otherwise the default value is just considered as default value for the first time you mount.. and that is almost useless.
I ended up doing stuff like this to achieve my goal:
This kind of beats the point of default values.
What do you think?
Looking forward to hearing from you,
Thank you for this amazing language
The text was updated successfully, but these errors were encountered: