React 模态打开第二个模态但第二个模态不起作用

React modal opens second modal but second modal doesn't work

我正在使用 GraphQl 后端开发一个 React 项目。我有一个模式,用户可以在其中查看有关主题的更多详细信息。在该模式中,您可以单击删除,这会打开一个新模式,您需要在其中确认是否要删除。当按下是时,它应该被删除并且模态应该关闭。当按下否时,模式应该关闭。删除主题有效。如果我按是删除它,但模态不会关闭,当我按否时,模态也不会关闭。谁能解释为什么以及如何解决这个问题?

父模态:

class CalenderModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = {

      openDeleteAppointment: false,

    };
    this.openDeleteAppointment = this.openDeleteAppointment.bind(this);
  }


  handleRemove = () => {
    this.props.onRemove();
  }

  openDeleteAppointment() {

    this.setState({
      openDeleteAppointment: true,
    })

  }


  render() {


    return (
      <React.Fragment>



          <div className="customModal">
            <div className="modal-header">

              <h5 className="customModal__text"> Appointment summary</h5>

              <button className="btn modal__button__red" onClick={() => { this.openDeleteAppointment() }}>Delete</button>

              {this.state.openDeleteAppointment &&
                <DeleteAppointmentModal appointment={this.state.id} onHide={() => this.setState({ openDeleteClient: false, id: null })} show />}

            </div>
            <div className="modal-container">

              <div className="summary">

              <button className="btn modal__button__cancel" onClick={this.handleRemove}>Cancel</button>
            </div>
          </div>


        }
      </React.Fragment>
    );



}

export default CalenderModal;

子模态:

class DeleteAppointmentModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            id: this.props.appointment,
        };

    }

    render() {
        const {id} = this.state
        const DELETE_MUTATION = gql`
   mutation DeleteMutation($id:ID! ) {
   deleteAppointment(id:$id) {
     id
   }
 }
     `
     console.log("delete id",this.state.id)
        return (
            <React.Fragment>
                {
                    <Modal
                        {...this.props}
                        size="lg"
                        aria-labelledby="contained-modal-update-client"
                        centered
                    >
                        <Modal.Header closeButton >
                            <Modal.Title id="contained-modal-title-vcenter" className="tittle">Delete appointment </Modal.Title>
                        </Modal.Header>
                        <Modal.Body>
                            <div className="delete-content">
                                Are you sure you want to delete this appointment?

                            </div>
                        </Modal.Body>
                        <Modal.Footer>
                        <button onClick={() => this.props.onHide() } className="btn no">No</button>
                            <Mutation mutation={DELETE_MUTATION}
                                variables={{id}}>
                                {/* onCompleted={() => this.props.history.push('/')} */}

                                {deleteMutation =>
                                    <button onClick={() => { deleteMutation(); this.props.onHide() }} className="btn yes">Yes</button>


                                }
                            </Mutation>

                        </Modal.Footer>
                    </Modal>

                }
            </React.Fragment>

        );
    }
}

export default DeleteAppointmentModal;

在父组件的 "return" 上更改以下行:

来自:

<DeleteAppointmentModal appointment={this.state.id} onHide={() => this.setState({ openDeleteClient: false, id: null })} show />}

至:

<DeleteAppointmentModal appointment={this.state.id} onHide={() => { this.setState({ openDeleteAppointment: false, id: null }); handleRemove(); }} show />}

希望它解决了问题。

来自观察:

  • 表演道具始终是 true。设置 show={this.state. openDeleteAppointment}
  • onHide 没有设置正确的状态。它应该设置 openDeleteAppointment 而不是 openDeleteClient