Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!