Skip to content

Nested component does not render properly in Svelte/Sapper

I have three files inside a slug. I use slug parameters as directory name. The problem I am having is everything except the each loop in taglist. For some reason it does not receive the prop tagList. Any help would be appreciated.


<script context="module">
    export function preload({ params }, { user }) {
        let [id, slug] = [, params.slug];
        return { id, slug };

    import Editor from "../../../_components/Editor.svelte";
    import Topics from "./Topics.svelte";
    import { stores } from "@sapper/app";

    export let id;
    export let slug;

    const { session } = stores();

    <title />

<div class="editor-page">
    <div class="container page">
        <div class="row">
            <div class="col-md-8 offset-md-2 col-xs-12">
                <Topics {id} {slug} />
                {#if $session.user}
                    <Editor />


    import { onMount } from "svelte";
    import * as api from "api.js";
    import "bytemd/dist/index.min.css";
    import TagList from "../../../_components/_TagList.svelte";

    export let id;
    export let slug;

    let topics = [];
    let title = "";
    let tagList = [];
    let value = "";
    let Viewer = null;

    onMount(async () => {
        const bytemd = await import("bytemd");
        Viewer = bytemd.Viewer;

        const response = await api.get(


        if (response.topic) {
            topics = response.topic;
            title = response.title;            
            value = topics[0].description;
            for(let i= 0; i < response.tags.length; i++) {

    <svelte:component this={Viewer} {value} />
    <TagList {tagList} />


    export let tagList;


    {#each tagList as tag}



In Svelte, updates are only triggered with an assignment.

In your case that means that when the component is rendered it will render an empty taglist (tagList = []).

Now in onMount you do taglist.push, but as said earlier, this doesn’t trigger an update (remember that this function is called after the component has mounted) because it is not an assignment.

There are four ways to fix it in your case:

  1. after the for loop you do tagList = tagList, this is an assignment and will trigger the update.
  2. instead of doing the for loop use a mapping tagList = => tag)
  3. instead of doing the for loop you spread the tags into the taglist tagList = [...response.tags]
  4. considering you don’t do anything with the tags anyway, and tagList is empty and you don’t seem to have any other way to update, just assign the tags to it directly tagList = response.tags

Of course your code might be simplified, if you actually do something with each tag before adding it to the list case 3 and 4 are not good options, for that scenario I would use the map option

User contributions licensed under: CC BY-SA
8 People found this is helpful