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
Add a ment  | 

2 Answers 2

Reset to default 7

https://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).

本文标签: javascriptHow to rerender each array block in svelte after you push something to an arrayStack Overflow