admin管理员组文章数量:1323707
i'm taking SveleteJS for a test drive and got stuck
Made a Dashboard
ponent and inside that ponent, i placed a Whiteboard ponent:
<script>
import Whiteboard from "./Whiteboard.svelte";
export let name;
</script>
<div class="box part-of-dashboard">
<Whiteboard lines={[]} />
</div>
Whitebord.svelte
:
<script>
export let lines = [];
export function addLine() {
lines.push("blah");
console.log(lines);
}
</script>
<div style="background-color:red">
{#each lines as line}
<div>
{line}
</div>
{/each}
</div>
<div>
<button on:click={addLine}>
Add Line
</button>
</div>
When i click the button, the console.log
triggers and i can see the lines increasing in size, but i don't see it being rendered on the page, just the empty red div wrapping it.
I've tried adding $:
to various places, but i'm not yet sure where it should be used and where it shouldn't be used, not that it was making a difference.
How do i get that #each to render a list of divs (and also, what is the correct way to pass in data from the on:click
, doing {addLine('blah')} executes that addLine on page load)?
i'm taking SveleteJS for a test drive and got stuck
Made a Dashboard
ponent and inside that ponent, i placed a Whiteboard ponent:
<script>
import Whiteboard from "./Whiteboard.svelte";
export let name;
</script>
<div class="box part-of-dashboard">
<Whiteboard lines={[]} />
</div>
Whitebord.svelte
:
<script>
export let lines = [];
export function addLine() {
lines.push("blah");
console.log(lines);
}
</script>
<div style="background-color:red">
{#each lines as line}
<div>
{line}
</div>
{/each}
</div>
<div>
<button on:click={addLine}>
Add Line
</button>
</div>
When i click the button, the console.log
triggers and i can see the lines increasing in size, but i don't see it being rendered on the page, just the empty red div wrapping it.
I've tried adding $:
to various places, but i'm not yet sure where it should be used and where it shouldn't be used, not that it was making a difference.
How do i get that #each to render a list of divs (and also, what is the correct way to pass in data from the on:click
, doing {addLine('blah')} executes that addLine on page load)?
1 Answer
Reset to default 9So, this kind of goes against what one might expect, but Svelte does not detect [].push()
as mutating the state of the array variable. This is why the console log shows the variable being updated, but Svelte's rendering does not respond.
After doing a little digging, I found several threads (1, 2) that point this out, and clarify that this is true for calling methods on any object.
EDIT: This is also now called out explicitly in the docs / tutorials - see "Updating Arrays and Objects"
One of the easiest solutions (per the linked threads and docs) is to simply re-assign the variable value to itself; Svelte will pick that up and re-render. So, in your scenario, all I had to do to get your code to work was replace this:
export function addLine() {
lines.push("blah");
console.log(lines);
}
With:
export function addLine() {
lines.push("blah");
lines = lines;
console.log(lines);
}
本文标签: javascriptSvelte each not looping through dataStack Overflow
版权声明:本文标题:javascript - Svelte #each not looping through data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130742a2422147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论