似乎最近 React 不会this.props.children像过去那样将其视为函数。我刚刚编写了一个Modal组件,它的closeModal功能应该传递给孩子,render() { return ( <Modal> { (closeModal) => { return (<span onClick={closeModal}>Click to close modal</span>) } } </Modal> )}模态看起来像这样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> ) }}我试图通过传递函数作为道具this.props.children({ closeModal: this.closeModal }),猜猜是什么,this.props.children根据最新的 React 16.9 不是函数。至于与GraphQL工作的人一个参考,我看到阿波罗客户<Mutation>和<Query>合作得非常大致相同的方式。如何实现?编辑:为什么不重复?因为其他答案依赖于this.props.children函数,而最近 React 现在呈现错误,需要一种新的方法来解决这个问题:TypeError: this.props.children is not a function
添加回答
举报
0/150
提交
取消
