为了账号安全,请及时绑定邮箱和手机立即绑定

将函数传递给 this.props.children

将函数传递给 this.props.children

慕容708150 2021-10-07 10:22:50
似乎最近 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
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 305 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号