Skip to content

Default data not re-added when removing property value #1467

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

Closed
jacobmischka opened this issue May 15, 2018 · 5 comments
Closed

Default data not re-added when removing property value #1467

jacobmischka opened this issue May 15, 2018 · 5 comments

Comments

@jacobmischka
Copy link
Contributor

jacobmischka commented May 15, 2018

The current behavior makes sense, but it would be great if default data could somehow be reinjected if previously-provided data gets removed. It can particularly get in the way when using spread for props.

REPL: https://svelte.technology/repl?version=2.6.1&gist=b2cd6467f026d0b3a0857d248d44f830

It may be sort of related to #1434, but I actually want the falsey values passed down, I just want the default data ones to re-take effect.

I guess I basically am wanting an implicit something like this:

onstate({ current }) { this.set(Object.assign(defaultData, current)); }

Edit: I guess using Object.assign for this doesn't work, becuase undefined is indeed a value which is overriding the defaults. I wonder if this is what's causing the issue in the first place.

Thanks!

@jacobmischka
Copy link
Contributor Author

jacobmischka commented May 15, 2018

Actually, it looks like defaultData already is re-added, it's just that changed[key] isn't set to true when a property is removed.

Ha my mistake, I thought that for a minute because I didn't realize svelte treats null the same as '' for interp, while it treats undefined as 'undefined'.

@TehShrike
Copy link
Member

@jacobmischka I think the misunderstanding here is that the data function doesn't return "default data" so much as it returns "initial data" for a component - the spread operator overwrites default in Nested there, but there's nothing that would cause the value of default to go back to 'here' once the spread operator doesn't have a value for that property and sets it back to undefined.

To get the behavior you want, a computed property is probably best. See this version, where Nested takes a value property, but references a computed property in the template in order to fall back to a default value: https://svelte.technology/repl?version=2.6.1&gist=0251ae7e167fc715a24d198d09aaaf51

@jacobmischka
Copy link
Contributor Author

That makes sense. I suppose this is more of a feature request than a bug report, because I think it would be nice if it behaved that way. Though you're right, computed properties could work just as well, thanks for the suggestion.

@TehShrike
Copy link
Member

Even if #1434 is implemented, I imagine it will be "don't touch the current values when undefined is passed in" and not "remember what the value was at some time in the past, and set it back to that value when the value is changed to undefined by some other code"

@Rich-Harris
Copy link
Member

Going to close this as the behaviour is expected. In v3 it's a good deal easier to patch objects as opposed to replacing them — instead of

this.set({nestedData: { added: 'updated!' }});

it's possible to do

nestedData.added = 'updated!';

thus leaving nestedData.default untouched.

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