亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將函數傳遞給 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 關注
  • 252 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號