When you create complex apps, there’s a high chance you need to create components that are nested in deeper components. This means there are several layers of components.
One simple way of illustrating this structure is to have layers of children components that need to access the main component’s count value.
When we click the button, we want to increase the count value in all descendant elements.
Tiny doesn’t support this now, so let’s find a way to make it work. The starter files for this lesson can be found in the link above.
The Structure
The structure for this demo is simple: We have a main component that passes props into the child.
We also have a Child Component that passes props into the a Descendant component. In this case, we need to pass props.count down since count is present in props.
Everything here is what I’ve written into the Starter file. They are logical and correct. But, if you open up page, you’ll notice the descendant component is missing.
There’s a simple fix.
Fixing the missing descendant
When we render children components previously, we did not account for further descendants being present inside each child component.
To fix this, we need to run _renderChildComponents again inside _renderChildComponents. This creates a recursive function that runs over and over again, until there are no more descendants.
export default function Tiny (options) {
// ...
function _renderChildComponents () {
const compEls = element.querySelectorAll('[tiny-component]')
for (const compEl of compEls) {
// ...
_renderChildComponents(childComp)
}
}
// ...
}