Skip to content
Advertisement

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.

index.svelte

JavaScript

Topics.svelte

JavaScript

_TagList.svelte

JavaScript

Advertisement

Answer

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 = response.tags.map(tag => 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
Advertisement