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

[docs] Update 02-template-syntax.md #7234

Closed
wants to merge 1 commit into from

Conversation

rodshtein
Copy link

Removed: > Note that we can't do {cart.empty} since cart is undefined when the button is first rendered and throws an error.

It fixed in 3.16.7
Issue: #4090

Removed: > Note that we can't do `{cart.empty}` since `cart` is `undefined` when the button is first rendered and throws an error.

It fixed in 3.16.7
Issue: sveltejs#4090
@bluwy
Copy link
Member

bluwy commented Feb 9, 2022

Duplicate of #6852. I still don't think this is a good idea according to #6852 (comment)

@bluwy bluwy closed this Feb 9, 2022
@rodshtein
Copy link
Author

@bluwy

Ok, it still confused.

Why we can't just use: on:click={cart && cart.empty} if is work. ( see #4090)
And why can't use: {foo.bar} no matter where on the page, before or after bind, but can use {foo.?bar}

Is there any way to describe it in the documentation?

@bluwy
Copy link
Member

bluwy commented Feb 9, 2022

Why we can't just use: on:click={cart && cart.empty} if is work.

You can use on:click={cart && cart.empty}, but for on:click={cart.empty} which also works, I don't recommend that as this exploits a compiler behaviour -- in cases where the event handler is a dynamic variable, Svelte will wrap it with an anonymous function, and indirectly guards again the potential error.

I think it's important to have the mindset that whatever event handler for on:click, the {...} will always be evaluated on the initial render. This prevents bugs in the long run.

And why can't use: {foo.bar} no matter where on the page, before or after bind, but can use {foo.?bar}

Going back to "{...} will always be evaluated on the initial render.", if you use {foo.bar}, it will error if foo is undefined. ?. syntax guards against it, it's basically sugar for {foo && foo.bar}.

I think the documentation has already described well with the blockquotes that this PR removes. As long as the blockquote recommendation is respected, I don't think we need to explain the internals given it's an implementation detail, and doesn't fit well in the docs.

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

Successfully merging this pull request may close these issues.

2 participants