Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
How would i make an each loop in svelte reactive

i have this svelte code:

    <!--connectprom is a Promise that resolves when connect is ran--->
    {#await connectprom}
    <!--User hasnt yet connected to the server-->
    <form on:submit|preventDefault={connect}>
        <label for="un">Username</label>
        <input type="text" id="un">
    </form>
    {:then}
    <!--After the form was submited (user is connected)-->    
    Welcome, <p class="name text-blue-300">{username}</p>
    <!--When send is ran it sends the data from the form to the socket.io server-->
    <form class="shadow-lg rounded-lg" on:submit|preventDefault={send} align="center">
        <label for="msg">Message:</label>
        <input type="text" id="msg">
    </form>
    {#each messages as msg}
    <Message username=msg.username content=msg.content/>
    {/each}
    {/await}

Is It Possible for the #each statement to run reactively? If not is there a workaround to do so?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Normally the {#each} statement is run reactively, and should update if the variable messages changes.

BUT, for Svelte to register an update in an array, you have to reassign the variable.

General example Code from the Svelte Docs:

numbers = [] //TODO: We want a function that adds an item to the end of the array, that is the array Length + 1, so after a few runs it should look like this: [1,2,3,4, ...]

//This doesn't work
function addNumber() {
    numbers.push(numbers.length + 1); //The variable is not reassigned
}

//This does work!
function addNumber() {
    numbers.push(numbers.length + 1);
    numbers = numbers; //This is the reassignment
}

// OR
function addNumber() {
    numbers = [...numbers, numbers.length + 1]; //And this is also the reassignment
}

And some Example Code for your problem:

messages = [] //TODO: Have a function that appends a message to the array and forces the {#each} "loop" to update

//This doesn't work
function addMessage(newMessage) {
    messages.push(newMessage); // It's just adding to the array but the array isn't "registered" as updated
}

//This does work!
function addMessage(newMessage) {
    messages.push(newMessage);
    messages = messages; //This is the reassignment and causes the update
}

// OR
function addMessage(newMessage) {
    messages = [...messages, newMessage]; //Another way to add to it and reassign it to also cause an update
}

So I think that you have to change the way you append messages to the messages array.

Reference and Examples: https://svelte.dev/tutorial/updating-arrays-and-objects

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda