It seems just recently React won't treat this.props.children
as a function as it did in the past.
I just coded a Modal
component where its closeModal
function should be passed to the children,
render() {
return (
<Modal>
{
(closeModal) => {
return (<span onClick={closeModal}>Click to close modal</span>)
}
}
</Modal>
)
}
Modal looks like this
class Modal extends React.Component {
constructor(props) {
this.state = { show: true }
this.close = this.closeModal.bind(this)
}
closeModal() {
this.setState({ show: false })
}
render() {
if (!this.state.show)
return null
return (
<div>
{ this.props.children }
</div>
)
}
}
I tried to pass function as a prop via this.props.children({ closeModal: this.closeModal })
, guess what, this.props.children
is not a function according to latest React 16.9.
As a reference for the folks working with GraphQL, I see Apollo client's <Mutation>
and <Query>
working quite much the same way.
How can it be achieved?
Edit: Why not a duplicate?
Because other answers rely on this.props.children
as function whereas recently React is now rendering error demanding a new approach to this issue:
TypeError: this.props.children is not a function