React's Children.only uses isValidElement under the hood. I guess we should same errors like within react.
Use case:
you component should be restricted to have one children and it should be a function ( render prop pattern via children )
This is valid within preact+preact-compat
class Test extends Component {
render() {
const children = Children.only(this.props.children);
if (typeof children !== 'function') {
throw new Error('we need 1 function human!');
}
return children();
}
}
in react it will on Children.only(this.props.children) when child's gonna be a function, because of mentioned isValidElement:
class Test extends Component {
render() {
// error
const children = Children.only(this.props.children);
if (typeof children !== 'function') {
throw new Error('we need 1 function human!');
}
return children();
}
}
React's Children.only uses
isValidElement
under the hood. I guess we should same errors like within react.Use case:
you component should be restricted to have one children and it should be a function ( render prop pattern via children )
This is valid within preact+preact-compat
in react it will on
Children.only(this.props.children)
when child's gonna be a function, because of mentionedisValidElement
:https://github.com/facebook/react/blob/master/packages/react/src/ReactChildren.js#L389