admin管理员组文章数量:1391850
Hello I'm making a ment section for my website which works but when I push an object to an array my {#each}
block doesn't update here is this block of code
{#each mentsScript as { userName, rating, ment }, i}
<div class="flex">
<div class="w-1/4">
<h5>{userName}</h5>
<h5>{rating}</h5>
</div>
<div class="w-3/4">
<p>{ment}</p>
</div>
</div>
{/each}
In my script section, I push a new ment to an array and it does show up after I refresh the page but I want to rerender it as soon as the user leaves a ment so the user would see hes/her own ment so is there any way I can rerender it?
Hello I'm making a ment section for my website which works but when I push an object to an array my {#each}
block doesn't update here is this block of code
{#each mentsScript as { userName, rating, ment }, i}
<div class="flex">
<div class="w-1/4">
<h5>{userName}</h5>
<h5>{rating}</h5>
</div>
<div class="w-3/4">
<p>{ment}</p>
</div>
</div>
{/each}
In my script section, I push a new ment to an array and it does show up after I refresh the page but I want to rerender it as soon as the user leaves a ment so the user would see hes/her own ment so is there any way I can rerender it?
Share Improve this question asked Feb 2, 2022 at 19:55 LaycookieLaycookie 74510 silver badges19 bronze badges 2- How are you updating the array? Provide code for that. – Samathingamajig Commented Feb 2, 2022 at 19:57
- 2 svelte.dev/tutorial/updating-arrays-and-objects You have to assign mentsScript = mentsScript if you are using push function. – Simon Harvan Commented Feb 2, 2022 at 20:00
2 Answers
Reset to default 7https://svelte.dev/tutorial/updating-arrays-and-objects
You have to assign mentsScript = mentsScript
, which would be redundant normally, but that is how svelte works. This is true if you are using push function on array.
In addition to reassignment, there are several existing libraries that help you use the Array native APIs.
You can try this: https://svelte.dev/repl/0dedb37665014ba99e05415a6107bc21?version=3.53.1
use a library called svelox. It allows you to use the Array native api(push/splice...etc.) without reassignment statements.
Because it's weird to write a reassignment every time, this is something that every svelte beginner finds strange. Also, using the Array native APIs is not remended(you should use spread).
版权声明:本文标题:javascript - How to rerender {#each array} block in svelte after you push something to an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764536a2623957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论