Skip to content

Components inside components

A component can use another component of your site inside itself: write the child's tag in the component's view, the way you would write it on a page.

render: (props) => (
<section>
<buy-button label={props.cta} rates={props.rates} />
<buy-button label="Gift" rates={[1, 2]}>
Buy
</buy-button>
</section>
);

Nothing is imported and nothing is declared. When the parent draws the child's tag, the site loads the child, at any depth: a component can draw one that draws one that draws one.

  • The child is what the site serves. Promote a new version of the child, or roll it back, and every component that draws it shows that version on the next page load. The parents are not published again.
  • Every tag is its own instance. The same child drawn five times with different props shows five different results, each with its own state, and its code downloads once.
  • A child that fails stays out of the way. If the child throws an error or its code does not load, the parent and the page keep working; the child's tag shows what you wrote inside it.

The site loads only the components attached to it, in the environment the page uses. Attach the child to the same site and environment as the parent. A child that is not attached is an empty tag: write the fallback inside it (<buy-button>Buy</buy-button>), exactly as on a page (fallback and slots).

  • Props are text, numbers, booleans, lists and objects, as in Props. A list or an object reaches the child even when the parent draws it before the child has loaded, and when the parent draws again the child gets the new values and keeps its own state.
  • A function is not a prop. The child announces what happened with an event it declares, and the parent listens to it: the event is named <tag>:<event> (buy-button:clicked) and is dispatched on the child's element.
  • Content goes through the child's slots, as on a page.
  • A component can sit at most 6 shadow roots deep. A component that draws itself through another (A draws B, B draws A) stops at that depth and the innermost element stays empty; the page is not affected.
  • At most 1,000 nested components mount on one page; past that, the extra ones stay empty. A page that nests a few per component never gets near it.
  • Server rendering draws the children too, in the same answer as the parent. A child it leaves out (the component cannot render without a browser, it is not on the site, or the answer is over a limit) stays an empty tag, and the browser draws it when the page loads.