简体   繁体   English

React:如何关闭从父组件打开的子组件的模态

[英]React: How to close a modal from child opened from parent component

I am opening child component modal by passing parent state as props to child.我通过将父状态作为道具传递给子组件来打开子组件模态。 Is there any way to close the modal from child component itself without any interference from parent component.有没有办法从子组件本身关闭模态而不受父组件的任何干扰。

class Parent extends Component {

    constructor(props) {
      super(props);
      this.showModal = this.showModal.bind(this);
      this.state = {
        showModal: false
      };
    }
    showModal() {
      this.setState({ showModal: true });
    }
    renderRow() {
      return (
        <tr>
          <td onClick={() => this.setState({ show: true })}>test</td>
          <ChildModal show={this.state.showModal}/>
        </tr>
      );
    }
}


class ChildModal extends Component {
  render() {
    return (
       <Modal show={this.props.showModal}>
            <Modal.Header closeButton> 
            <Modal.Title>Test</Modal.Title> 
            </Modal.Header>
            <Modal.Body> 
                {/* some text */}
            </Modal.Body>
        </Modal>
    );
  }
}

I want my child modal to be self contained.我希望我的孩子模态是独立的。 Is this even possible in react.这在反应中甚至可能吗?

You need to pass a callback as a props in Child component , it will update Parent Component when you click on closeButton in child .您需要将callback as a props Child component callback as a props传递, when you click on closeButton in child Parent Component when you click on closeButton in child ,它会更新Parent Component

// Parent Component
callbackModal = () => {
   this.setState({ showModal: false });
}
//ChildButton
closeButtonClickHandler = () => {
  this.props.callbackModal();
}

A local state variable can only be controlled inside the component in which it was declared in.局部状态变量只能在声明它的组件内部进行控制。

You will not be able to close the modal from inside the child component without passing a method from the parent component that changes the state that is passed down.如果不从父组件传递更改传递状态的方法,您将无法从子组件内部关闭模态。

So in order to close your modal, you'll need to create a method this.closeModal and pass it from the Parent to the child...所以为了关闭你的模态,你需要创建一个方法this.closeModal并将它从父级传递给子级......

// Parent
closeModal = () => {
  this.setState({showModal: false});
}
// ...
<ChildModal show={this.state.showModal} handleClose={this.closeModal} />
// ... 

Yes, you can close it from the child component, but you'll need at least a little interference of parent component, and that is because you've defined the toggle state of this model in parent component.是的,您可以从子组件关闭它,但是您至少需要父组件的一些干扰,这是因为您已经在父组件中定义了该模型的切换状态。

simply define a method that will close the modal in parent component, pass it down to the child component as a prop, and call it there.只需定义一个方法,该方法将关闭父组件中的模态,将其作为道具传递给子组件,并在那里调用它。

//in your parent component 
handleModalClose = ()=>{
this.setState({showModal: false})}

now pass it down to your child component and simply call it there on an event like现在将它传递给您的子组件,然后在类似的事件中调用它

this.props.handleModalClose()
class Parent extends Component {

constructor(props) {
  super(props);
  this.showModal = this.showModal.bind(this);
  this.closeModal = this.closeModal.bind(this)
  this.state = {
    showModal: false
  };
}
showModal() {
  this.setState({ showModal: true });
}
closeModal() {
  this.setState({ showModal: false });
}
renderRow() {
  return (
    <tr>
      <td onClick={() => this.setState({ show: true })}>test</td>
      <ChildModal show={this.state.showModal} close={this.state.closeModal}/>
    </tr>
  );
}
}

class ChildModal extends Component {
  render() {
    return (
       <Modal show={this.props.showModal}>
            <Modal.Header closeButton> 
            <Modal.Title>Test</Modal.Title> 
            </Modal.Header>
            <Modal.Body> 
                <buttom onClick={this.props.closeModal()}> ......
            </Modal.Body>
        </Modal>
    );
  }
}

When you define a state in a parent component, pretty much everything around that state is handled from there.当您在父组件中定义状态时,该状态周围的几乎所有内容都从那里处理。 The child component can only receive data from the parent component in the form of props.子组件只能以 props 的形式从父组件接收数据。

The parent component controls the opening and closing state of the modal so for you to be able to close the modal from the child component, you have to define a function closeModal on the parent component which will set the showModal variable (defined in the state of your parent component) from true back to false.父组件控制模态的打开和关闭状态,以便您能够从子组件关闭模态,您必须在父组件上定义一个函数closeModal ,该函数将设置showModal变量(在状态中定义)你的父组件)从真回到假。

closeModal = () => {
      this.setState({ showModal: false });
    }

Then pass this function as props to the child component and call the function from there.然后将此函数作为道具传递给子组件并从那里调用该函数。 When you click on the close button on the modal, the state will be updated on the parent component.当您单击模态上的关闭按钮时,状态将在父组件上更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM