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

150
Vistas
Cannot render elements from each block

I'm trying to render msg from all_msgs array.

<script>
  import { sender_msgs } from "../var_store";
  import { receiver_msgs } from "../var_store";
  const all_msgs = [1,2,3,4];
  // $: msgs = all_msgs;
  sender_msgs.subscribe((e) => {
    all_msgs.push(`Sender: ${e.slice(-1)[0]}`);
  });
  receiver_msgs.subscribe((e) => {
    all_msgs.push(`Receiver: ${e.slice(-1)[0]}`);
    console.log(all_msgs);
  });
</script>

<div class="chat-window">
  {#each all_msgs as msg}
    <div>{msg}</div>
  {/each}
</div>

<style>
  .chat-window {
    width: 500px;
    border: 1px solid #000;
  }
</style>

I can see the numbers can be rendered as html text. But couldn't render other texts when all_msgs is updated by the two subscribe methods. I can see all_msgs in the console having the texts but can't be seen in html. The o/p of the screen and console.log of all_msgs is also shared. enter image description here

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

0

Solved it: reactivity is assignment based. Below code works:

<script>
  import { sender_msgs } from "../var_store";
  import { receiver_msgs } from "../var_store";
  let all_msgs = [];
  // $: msgs = all_msgs;
  sender_msgs.subscribe((e) => {
    all_msgs.push(`Sender: ${e.slice(-1)[0]}`);
    all_msgs = all_msgs;
  });
  receiver_msgs.subscribe((e) => {
    all_msgs.push(`Receiver: ${e.slice(-1)[0]}`);
    all_msgs = all_msgs;
  });
</script>

<div class="chat-window">
  {#each all_msgs as msg}
    <div>{msg}</div>
  {/each}
</div>

<style>
  .chat-window {
    width: 500px;
    border: 1px solid #000;
  }
</style>
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