Code Monkey home page Code Monkey logo

Comments (9)

Rich-Harris avatar Rich-Harris commented on July 18, 2024 1

writing to an object's prop doesn't count as reading the object

If that were the case, clicking the second button would cause {object.count} inside the first to become undefined (because the effect wouldn't re-run after assigning to object), which would obviously be a bug:

<script>
  let object = $state({});
  let count = $state(0);

  $effect(() => {
    console.log('in effect');
    object.count = count;
  });
</script>

<button onclick={() => count++}>
  clicks: {object.count}
</button>

<button onclick={() => object = {}}>
  reset object
</button>

from svelte.

Rich-Harris avatar Rich-Harris commented on July 18, 2024 1

That's absurd — the resulting behaviour would make no sense at all! And what if you wrote it like this, for whatever reason? Does object suddenly become a dependency of the effect, because it's no longer immediately assigned to?

$effect(() => {
  const obj = object;
  obj.count = count;
});

Or does some magical static analysis happen that somehow determines that object isn't actually "used", it's only written to? In which case what happens here?

function update_object() {
  const obj = object;
  obj.count = count;
}

$effect(() => {
  update_object();
});

There's only one sensible behaviour here, and it's the one that's currently implemented. The solution here, if you really need to resort to using an effect (i.e. you can't bind directly to condition.playerId or whatever) is to use untrack.

from svelte.

frederikhors avatar frederikhors commented on July 18, 2024

Maybe I should use <select bind:value={condition.playerId}>, right?

from svelte.

7nik avatar 7nik commented on July 18, 2024

playerId gets updated to "1" probably because there is no undefined among the options. Then, in effect, you modify condition, and it seems $effect subs to condition as well, causing an infinite loop.

Though it's incorrect usage of $effect, it looks like a bug that $effect subs to condition despite on only writing to it.

from svelte.

Rich-Harris avatar Rich-Harris commented on July 18, 2024

This is expected behaviour — condition is state, and you're reading and writing to it in the same effect

from svelte.

Rich-Harris avatar Rich-Harris commented on July 18, 2024

(use untrack if you want to, well, untrack the read)

from svelte.

7nik avatar 7nik commented on July 18, 2024

Wasn't there a fix that writing to an object's prop doesn't count as reading the object?

Edit:

	let obj = $state({});

	$effect.pre(() => {
		obj.prop = {}
	});

	$inspect(obj); 

no loop, but

	let obj = $state({});

	$effect.pre(() => {
		obj = {}
		obj.prop = {}
	});

	$inspect(obj); 

causes loop

from svelte.

brunnerh avatar brunnerh commented on July 18, 2024

I neither think that is obvious, nor that it necessarily should work that way.
To me the effect reads as having one dependency: count

from svelte.

brunnerh avatar brunnerh commented on July 18, 2024

Just goes to show that effects are not particularly intuitive.
You also don't need untrack here since the whole object is assigned.

  $effect.pre(() => {
- 	condition = { };
+ 	const newCondition = { };
  	if (playerId) {
-  		condition.playerId = playerId;
+  		newCondition.playerId = playerId;
  	}
+ 
+ 	condition = newCondition;
  });

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.