Skip to content

Svelte 3/4: Dynamic event handler function can be falsely deferred. #12519

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

Open
brunnerh opened this issue Jul 21, 2024 · 0 comments
Open

Svelte 3/4: Dynamic event handler function can be falsely deferred. #12519

brunnerh opened this issue Jul 21, 2024 · 0 comments

Comments

@brunnerh
Copy link
Member

brunnerh commented Jul 21, 2024

Describe the bug

To account for event handlers changing at runtime, handlers that come from props or variables that are reassigned are not used directly as event handlers. Instead, their invocation is wrapped in a new function, which can be wrong if a function call is used to get the handler. See:

(Not sure if something can/should be done about this at this point, it primarily would be good, if the problem is not re-introduced in Svelte 5.)

Reproduction

<script>
	let makeHandler = null; // could also come from a prop
	makeHandler = () => {
		console.log('Creating handler');
		return () => alert('hello');
	};
</script>

<button on:click={makeHandler()}>Click</button>

REPL

"Creating handler" should only be logged once, but the function is executed on every click.

Logs

No response

System Info

REPL

Severity

annoyance

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

1 participant