By Alex Jover Morales

In the Recursive Components in Vue.js post weβve seen how to build a recursive Tree component, but we didnβt have to communicate any action across the tree. Letβs tackle that problem in this post.
Imagine that you want to check when a NodeTree from the previous article gets clicked-on, and communicate it to the outer component (the one whoβs using the Tree component).
In Vue.js, upwards communication happens using custom events. But weβve learned from Vue.js 1.x or Angular 1.x that having events across multiple levels is a bad practice and makes for a codebase thatβs hard to reason about. Thatβs why, starting with Vue.js 2, custom events are only allowed on one child-to-parent level.
Knowing that, imagine a Tree like the following:
+ Folder 1
+ Folder 2
+ Folder 3
+ Folder 4
+ Folder 5
If weβd like to communicate a click event from Folder 5 using custom events, weβd have to emit an event up 5 times.
We can say a tree has infinite levels of depth, since we donβt know beforehand how many it has. For that reason, using custom events would make it unfeasible, inefficient and complex.
Remember that we can use functions as props in Vue.js. Itβs not a common pattern because, unlike with React, Vue.js has custom events for child-to-parent communication. But for cases like this one it really comes in handy.
Unlike emitting events at each level, this is much simpler and performant since we only need to pass down the same reference to the function.
Letβs add a handleClick property to the NodeTree.vue component, and use it for the click event:
<template>
<li class="node-tree">
<span class="label" @click="handleClick(node)">{{ node.label }}</span>
<ul v-if="node.children && node.children.length">
<node
v-for="child in node.children"
:node="child"
:handle-click="handleClick">
</node>
</ul>
</li>
</template>
<script>
export default {
name: "node",
props: {
node: Object,
handleClick: Function
}
};
</script>
Notice how we set its type as Function and we use it on the the label @click event and pass it down again to the children nodes :handle-click="handleClick".
From the Tree.vue component, we still need to pass that function down to the root node.
Additionally, we must provide a way to handle the click from the outside (the component who uses Tree), and for this we can indeed use a custom event since itβs just one level up:
<template>
<div class="tree">
<ul class="tree-list">
<node-tree :node="treeData" :handle-click="handleClick"></node-tree>
</ul>
</div>
</template>
<script>
import NodeTree from "./NodeTree";
export default {
props: {
treeData: Object
},
methods: {
handleClick (node) {
this.$emit('node-click', node);
}
}
components: {
NodeTree
}
};
</script>
As you can see, we added a handleClick method local to the Tree component, which is passed to the node-tree. This method emits an event with the node of data by using the $emit Vue instance method.
With that, we can handle the click from App.vue or any other external component, while keeping a nice API syntax by using @node-click on the template:
<template>
<div>
<tree :tree-data="tree" @node-click="logClick"></tree>
</div>
</template>
<script>
import Tree from "./Tree";
export default {
data: () => ({
// ...
}),
methods: {
logClick(node) {
console.log("Clicked: ", node);
}
},
components: {
Tree
}
};
</script>
As you just saw, to fix the βinfinite levelsβ communication problem, weβve made use of a pattern thatβs very common in the React community: passing a function down as a property. This allowed us to handle a click action easily on a recursive component while keeping it performant and easy to reason about.
Stay cool π¦
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Scale up as you grow β whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.
