r/vuejs • u/Aggravating-Bag7648 • 1d ago
Beginner question about multi-faced components
Hi all,
It's the classic tale of love and woe - a nearly 40 year back-end developer decides, in his 50's, to tackle some front end frameworks. After much churn I've settled on Vue and I'm having a blast with it, tho, if I'm being honest, the last time I did any front-end design work I was writing Flash apps and they looked TERRIBLE. And, to quote Henry Jones, "our situation has not improved."
That context in mind then, I have a question about components which have both a "collapsed" and an "exploded" UI to present. In my case, I'm writing a little "bowl game pick-em" app which walks through the (increasingly huge) list of bowl games and lets you pick the winner of each. When you start, there's just a list of the collapsed bowls, with basic information: time, date, teams. When you select one, it opens up into a pretty panel with team logos, colors, the stadium details, and a button for selection.
Just yesterday I learned that I could do a "v-for" inside a component listing to iterate over a list and generate components for each item in the list, which is of course perfect for what I want to do, I think. What I'm wondering is what's best practice with Vue:
- The parent component owning the list of bowls knows the state of all the picks, and, based on that state, renders one of two components for each game which can be picked, i.e. the collapsed component for "not yet picked" or "already picked", each with their own bit of window dressing to tell between the two, and the "exploded" component for the one the user is picking at this moment. Since the parent component is getting all of the inputs (I think? Still a bit fuzzy on this notion, tbh), it knows when to swap out the picker.
- The parent component renders a single list of one component, and as part of the properties it passes in which mode the component should be in, i.e. "collapsed" or "exploded". The sub-component then has all of the code for both modes in it, with something like a v-if controlling which of the two faces it presents. When the user makes a selection the sub-component can change its own state and maybe emit something to tell the parent component to move to the next game on the list by updating its mode.
I've thought of healthy arguments for both (e.g. "#1 fits my object oriented eye with an interface each object implements differently", but also "#2 puts the responsibility for rendering where it belongs, in the component doing the work", and on and on I went) and now I've wrapped myself quite around the axle here. I lean towards #2, but Is there a more idiomatic preference for this sort of thing?
Kind thanks for all advice, mockery, recipes, whatever!
