Image for post
Image for post

A Recipe for Nesting Components Within a List in Vue.js and Vuex

So, this situation baffled me for a while. In my state object, I have a list of keys, and then I have an object that contains the data for all the keys. It looks like so:

The first question a sensible person might ask is: why use this data structure at all? Why not simply have a list of items?

In my real world scenario, I have multiple lists. I want to be able to re-order the lists, move items from one list to another, create items, edit items, etc. All of this becomes at least conceptually easier when an item can be referred to by its id, rather than finding it in the correct list and referencing it by list-and-position. The items object is the authoritative model of data; the list array is simply a tool of convenience.

All well and good; now I want to display a list of my items. My first component, then is the list component itself:

With a root level element that looks like the following, this works like a champ.

In the markup, Vue’s :list directive instructs Vue to make the state.list data available to the property list which is defined in the component as requiring an Array. All good!

Where I ran into trouble was in desiring to iteratively include a component for each list item which would display the actual item data! What are the correct ways to get from the key in the array to the full item in a nested component?

I’ll spare you all the horrible syntactical convolutions that I tried and failed with. Here’s the short, sweet answer (although not quite the best one, as you shall see).

Here’s the component itself:

The two crucial points of wiring are the prop for ikey which is where we are going to receive the key from the parent component, the list itself, and the data section that locates the item in our store, given that ikey. You might wonder why I am using ikey instead of key. I initially tried to use key, but Vue threw errors. It is a reserved word in Vue. So I renamed my property and moved on. But this is going to come back very significantly later.

We now need to change the template in our list to properly reference this component, and complete the circuit. Now:

As before, we tell Vue to allocate the value of the key our parent component is using for the property ikey, which our component code requires and expects as a String; the component’s data function then uses that property to obtain the authoritative record.

As easy as that, the job is done.

But it turns out there is something missing, once our application gets more complicated.

I added functionality to delete an item from our list. I won’t go into all that code, for the most part it is easy and obvious. But I encountered a baffling bug. The item would be correctly removed from the list, and the list display would update with an item removed… but not (necessarily) the right item!

The biggest mystery in this was that taking an action on the item that I thought I was removing revealed it to be an item that was not removed! Somehow the list display had become unmoored from the underlying data source.

Which, as you may imagine, is where that :key keyword comes in.

Revise our list template code one more time:

Now, any actions taken on the underlying list correctly update the list and the display of items. Voila!

There’s just one little thing… do we really need both a key and an ikey? No, no we don’t. But key takes a little extra magic to access from within the component. Let’s update the template and the component one last time, and for full effect, put it all in a single single-file fully working example. Because gist hates me and won’t tab correctly, you get this one without syntax highlighting.

The final trick, then is to dispense with the property entirely and reference key not as a property, but as a Vue magic variable, off the $vnode object.

Please let me know if this was of benefit to you, or if you see any ways it can be made more elegant!

Image for post
Image for post
Isn’t it nice to code in a coffee shop? My favorite. None of these people are me. It’s a stock photo.

Other Articles in the Vue Series:

Written by

Building web applications since 1992. Crikey, that’s a long time.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store