Skip to content
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 properties in parent component default to "undefined" when omitted with spread #3063

Closed
lud opened this issue Jun 20, 2019 · 4 comments

Comments

@lud
Copy link

lud commented Jun 20, 2019

Simple demo here : https://svelte.dev/repl/d0476b3598e648cfbe38922617a8798c?version=3.5.3

Basically, if you have an object that is used as spread props for a child component, and you overwrite the variable with a new object with different keys, the keys that were present and are missing takes precedence as undefined over the child component default values.

@btk5h
Copy link
Contributor

btk5h commented Jun 20, 2019

Is this a bug? This behavior makes sense to me since the code inside the script block is only executed once. export let title = 'No title' is more of an initial value rather than a default/fallback value.

You could use the || operator to provide a fallback value if one of the props becomes undefined after the component is mounted.

@lud
Copy link
Author

lud commented Jun 20, 2019

I see. I may have misiterpreted how things work here. Is there a simple way to force a nested component to recreate ?

@alfredomariamilano
Copy link

That's expected vanilla JS behaviour. Either you use subProps.text = 'Some text' or subProps = { ...subProps, text: 'Some text' }.

@lud
Copy link
Author

lud commented Jun 21, 2019

The combination of $: and || is good enough :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants