A super simple implementation of this component would be:
1 2 3 4 5 6 7 8 9101112
function Switch(props) {
let children = props.children;
if (!Array.isArray(children)) children = [children];
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.props.when) return child;
}
return props.fallback;
}
For example, it can be used to perform basic routing: