svelte: derived value is not updating correctly
Describe the bug
the derived value depends on a class field of a newly created class. when this derived value has a fallback value (like “v || ‘-’”) it doesnt update correctly. removing the fallback seems to fix the issue.
Reproduction
https://svelte.dev/playground/dec1b43d47eb4c3aa729291f91ab8a39?version=5.28.2
Logs
System Info
System:
OS: Windows 11 10.0.22631
CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
Memory: 21.19 GB / 31.91 GB
Binaries:
Node: 22.12.0 - ~\AppData\Local\fnm_multishells\15692_1745479128899\node.EXE
Yarn: 1.22.22 - ~\AppData\Local\fnm_multishells\15692_1745479128899\yarn.CMD
npm: 11.3.0 - ~\AppData\Local\fnm_multishells\15692_1745479128899\npm.CMD
pnpm: 10.7.0 - ~\AppData\Local\fnm_multishells\15692_1745479128899\pnpm.CMD
Browsers:
Edge: Chromium (133.0.3065.82)
Internet Explorer: 11.0.22621.3527
Severity
annoyance
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (13 by maintainers)
Commits related to this issue
- fix: ensure unowned deriveds can add themselves as reactions while connected Fixes #15829 and potentially #15853 — committed to sveltejs/svelte by dummdidumm a year ago
- fix: ensure unowned deriveds can add themselves as reactions while connected (#16249) an unowned derived should still add itself as a reaction while it is properly connected, which means it can be cl... — committed to sveltejs/svelte by dummdidumm a year ago
So a bit more exploration and the problem is that
skip_reactionis true when running thederivedbecause it’sUNOWNEDthere’s noactive_reactionbefore and this line to check there was no parent was removed in #15232Adding the line back obviously is a no-go as it make that test fail. I’ll try to figure out if there’s another solution but it’s getting late lol
The reason why this works is because
this.c1?.v || '-'actually returns twice the same value whenvis0and that’s the root issue.Basically: the value of the derived is
-beforec1is defined in thesetfunction and it’s also-afterc1is defined…this means that thetemplate_effectthat updates the text in the dom is not executed and it never register a dependency onvso when it changes it doesn’t re-execute.Gonna explore this a bit more but it’s hard to wrap your head around.