DEV Community

Creating Tabs component in Vue 3

Zafar Alam on August 01, 2021

Hi! In this post we are going to walk through how to create a Tabs component in Vue 3. The main aim for this post is for me to get started with wr...
Collapse
 
karnie6 profile image
Karthik Senthil

Hi @zafaralam - this is great. When using this, I'm having difficulty rendering a dynamic list of (e.g. i'm using . It seems that does its magic at a different part of the lifecycle and doesn't recompute - as a result, I don't see any tabs. Any ideas on this?

Collapse
 
zafaralam profile image
Zafar Alam

Thanks @karnie6
I did realise a little bug myself. I'll update the code in the repo and the blog post to reflect the change.

Collapse
 
zafaralam profile image
Zafar Alam

@karnie6 I just realized that you were trying to render a dynamic list. In the Tabs.vue file you should see the onMounted method loads the tabs list. You will need some way to modify that, such as using a watcher.

If you post an example in the issues for the repo, I could help a bit more.

Good luck!!

Thread Thread
 
karnie6 profile image
Karthik Senthil

Hi @zafaralam - appreciate the quick response! I'll take a look at the onMounted and see if i can run the same code for onUpdated. I also realized my initial post didnt have much detail.

What I'm trying to do is iterate through an array and dynamically render elements under , one for each item in the array. Here's an example using a fixed array (testArray of 2 elements): github.com/karnie6/helpdesk-nft-ar...

When I run this, I don't see any tabs render. Inspecting the HTML, you see the HTML from the element but the HTML that's supposed to be generated from the element isn't there. However, if I hardcode two elements, it works. I agree with your suspicion it's a lifecycle issue of some sort.

Appreciate any help you can provide!

Thread Thread
 
zafaralam profile image
Zafar Alam

Hi @karnie6 I've updated the tabs component and it should now render the tabs dynamically. Look at the examples 7 & 8 in the App.vue file for how you can use it.

Hope this helps and thanks again for providing an example.

Thread Thread
 
karnie6 profile image
Karthik Senthil

hey @zafaralam - AMAZING! Looks like it works, thanks so much for the quick fix!

FYI, I'd love to share what I'm building with you - if you're interested, wanna email me at karthik dot senthil at gmail ? Thanks!

Thread Thread
 
karnie6 profile image
Karthik Senthil

@zafaralam - one other question. If i wanted to refresh the tab content on tab click, what's the best way to do that? Does have to listen to the 'tabChanged' event and do a force refresh? If so, what way would you recommend implementing that?

Thread Thread
 
zafaralam profile image
Zafar Alam

I would hook into the tabChanged event and then write my logic in the component that has the tabs in it.

This way you will be able to better control the refresh behaviour and it will be decoupled from the tabs component.

Also my email is syed.alam@live.com.au or you can reach out to me on twitter @zafalam.

Thread Thread
 
karnie6 profile image
Karthik Senthil

Got it to work - and just followed you on Twitter!

Collapse
 
mreduar profile image
Eduar Bastidas

It would be great if you could make a version without ts and without scss. Not everyone uses these tools. Thanks for sharing

Collapse
 
zafaralam profile image
Zafar Alam

I've been thinking about making version without ts and scss but just haven't got around to it. You can use most of the TS code as JS and just remove the typing. For scss to CSS you can use an online converter.

I'll try to create a version but no promises.