Building a flexible Card with optional content
flexible component APIs via props or composition.
use optional props or named slots for image, title, and text, conditionally render present ones, and prefer composition for ordering.
What's really being asked
This evaluates how you build a flexible, reusable component without overcomplicating its API, and whether you reach for composition when prop-driven configuration would get unwieldy.
The full answer
For a simple Card, accept optional props for each region: an image source, a title string, and body content via children. Render each region only when its prop is present, so omitting the image simply skips that markup. This handles which elements show cleanly. The harder requirement is order. Controlling order through props alone tends toward an explosion of configuration, so the better answer is composition: expose subcomponents such as Card.Image, Card.Title, and Card.Body that the consumer arranges in whatever order they need inside the Card. The Card supplies consistent padding, borders, and spacing, while the consumer composes the pieces. If you must stay prop-driven, accept that order is fixed by the Card or pass a small ordered array describing the layout, but call out the tradeoff. Avoid a pile of boolean flags like showImage and showTitle, which signals the API should be composition instead.
The mistakes people make
A boolean for every element plus a hardcoded order, which is rigid and grows badly. Forcing all three regions to always exist. Trying to encode arbitrary ordering through many props instead of letting the consumer compose.
What usually comes next
When do you choose props versus compound components? How do you keep consistent spacing when slots are optional? How would you support a horizontal versus vertical Card variant?
A concrete example
Card exposes Card.Image, Card.Title, and Card.Body. A product card composes image then title then body; a testimonial card omits the image and puts body above a title. Both reuse the same Card shell for borders and padding, and ordering is just the order the consumer writes the children.
Interview question
A Card must support optional image, title, and text in varying order. Why is a compound-component API often better than many boolean show-flag props?
- a.It makes the component render faster by skipping reconciliation
- b.It removes the need to style the Card consistently
- c.It lets consumers control presence and order directly, avoiding a rigid flag-driven layoutCorrect
- d.It prevents consumers from omitting any of the three regions
Why? this is the answer
Composition lets the consumer include and order pieces freely, which boolean flags plus a fixed order cannot do without bloating the API. It does not change rendering performance or styling responsibility.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
- #component-composition
- #react
- #props
- #compound-components
- #reusability
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles