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

Assignments inside {#if} cause inconsistent state #6007

Closed
CherryDT opened this issue Feb 20, 2021 · 5 comments
Closed

Assignments inside {#if} cause inconsistent state #6007

CherryDT opened this issue Feb 20, 2021 · 5 comments
Labels

Comments

@CherryDT
Copy link

CherryDT commented Feb 20, 2021

Describe the bug
Assignments inside of an {#if} (which can be useful if the value being assigned is the return value of a function and it should be checked and used without calling the function twice) create an inconsistent state: The correct value is rendered, but event handlers see an old value.

To Reproduce
Check this REPL: https://svelte.dev/repl/f24c001c7e284906a61d215b02dd0928?version=3.32.3

<script>
	let name = "foo";
</script>

{#if (name = "bar")}
	<button on:click={() => alert(name)}>I should alert "{name}"</button>
{/if}

Note that the alert says foo when clicking the button, despite the button text itself saying bar.

Expected behavior
Alert should say bar.

Severity
Not causing any immediate issue for me, I can work around it.

@Conduitry Conduitry added the bug label Feb 20, 2021
@Conduitry
Copy link
Member

The {#if} block is just resulting in (/*name*/ ctx[0] = "world") - presumably it should also generate an $$invalidate call like other expressions in the template do.

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Jun 26, 2021
@CherryDT
Copy link
Author

Since it has already been confirmed and labeled as bug, I don"t think it makes sense to close this as stale just because it didn"t make it out of the backlog yet...

@stale stale bot removed the stale-bot label Jun 26, 2021
@FelixMo42
Copy link

It look like this bugs still here in the newest version. I"ll take a crack at it, but I"m new so I would appreciate pointers if anyone knows where I should look.

@gtm-nayan
Copy link
Contributor

Assignments and mutations inside the template are undefined behaviour, see #7295 and the linked issues there

Closing as duplicate of #3793

@gtm-nayan gtm-nayan closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants