繁体   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