Skip to content

I want to set a Vairable only once when a $State variable changes #15600

Discussion options

You must be logged in to vote

You could add a utility flag:

let isChanged = $state(false)
let mounted = false;
$effect(() => {
	$state.snapshot(data);
	if (mounted)
		isChanged = true

	mounted = true;
});

Playground

Logic like this could be extracted to a separate function in a .svelte.js file.

The callback added in this PR might also help later:

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@Keshav-writes-code
Comment options

Answer selected by Keshav-writes-code
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants