簡體   English   中英

React:關閉來自Parent的Child Component中的Modal

[英]React: Closing Modal in Child Component from Parent

我在子組件中有一個模態,用於處理父組件中的刪除功能。 孩子持有模態的狀態(打開或關閉),因為這似乎是最合乎邏輯的地方。

 removeItem() {
   console.log('clicked');
  };

 ...

 <DeleteButton deleterecord={()=>this.removeItem(data.row._original._id)}/>

兒童

close() {
  this.setState({ showModal: false })
};

open() {
  this.setState({ showModal: true })
};


render() {

 return(
  <div>
    <Button
    bsStyle="primary"
    bsSize="small"
    onClick={this.open.bind(this)}
  >
    Delete
  </Button>

  <Modal
    show={this.state.showModal}
    onHide={this.close.bind(this)}
    bsSize="small"
    >
   ...

在removeItem代碼運行后,如何從父級關閉模式。

你可以用ref來調用子關閉函數嗎?

    removeItem() {
       console.log('clicked');
       this.child.close();
    }

   render() {
      return (
         <div>
            <ChildWithModal ref={(ref) => { this.child = ref; }} />
         </div>
      );
   }

兒童

...

close() {
   this.setState({ showModal: false })
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM