【问题标题】:Open the modal from different component in react从反应中的不同组件打开模态
【发布时间】:2020-05-20 19:40:24
【问题描述】:

我的模态:

class DeleteModal extends Component<Prop, State> {
  state = {
    deleteConfirmModel: false
  };

  handleModalToggle() {
    this.setState(({ deleteConfirmModel }) => ({
      deleteConfirmModel: !deleteConfirmModel,
    }));
  };
  Service = new Service();
  delete = () => {
    this.Service
      .delete()
      .then((response) => {
        window.location.reload(false);
      })
      .catch((error) => {
        console.log(error);
      });
  };
  render() {
    const { deleteConfirmModel } = this.state;
    return (
     <React.Fragment>
        <Modal
          isSmall
         title="Confirmation"
         isOpen={deleteConfirmModel}
        onClose={() => this.handleModalToggle()}
          actions={[
            <Button
              key="confirm"
              variant="primary"
              onClick={this.delete}
            >
              Delete
            </Button>,
              <Button key="cancel" variant="link" onClick={this.handleModalToggle}>
              Cancel
            </Button>

          ]}
        >
          Are you sure you want to delete the item?
        </Modal>
       </React.Fragment>
    );
  }

我的其他组件:

export class SampleCard extends Component<Prop, State> {
  state = {
    deleteConfirmModel: false,
  };
  handleModalToggle = () => {
    this.setState(({ deleteConfirmModel }) => ({
      deleteConfirmModel: !deleteConfirmModel,
    }));
  };

  render() {
    const { deleteConfirmModel } = this.state;

    return (
      <React.Fragment>
        <Row>

            <Card
              isCompact={true}
              isSelectable={true}
              className={"productListing"}
            > 
                <Button
                  onClick={() => this.handleModalToggle()}
                  isDisabled={false}
                > Delete Modal</Button>

            </Card>

        </Row>
      </React.Fragment>
    );
  }
}

从我的 SampleCard 组件中,我想在单击 Delete 按钮时打开我的 deleteModal 组件。任何人都可以帮忙解决。因为当我在 SampleCard 组件中编写代码时,出现以下错误: 没有与此调用匹配的重载。 重载 1 of 2, '(props: Readonly):"

【问题讨论】:

  • 你从哪里得到错误?如果是关于您的状态,请尝试将其设置为 setState(prevState =&gt; ({...prevState, deleteConfirmModel: !prevState.deleteConfirmModel})
  • 所以当我在 sampleCard 组件中编写 时,我收到错误消息:没有重载匹配此调用。重载 1 of 2, '(props: Readonly):"

标签: javascript reactjs typescript


【解决方案1】:

在尝试重现它时,我注意到您错过了模态组件上的 export 关键字。

deleteConfirmModEl => deleteConfirmModAl 也有错字。

修复它并简化您的代码后,它对我有用。这是the repro on Stackblitz,如果它不正常,代码:

样品卡:

import React from "react";
import DeleteModal from "./DeleteModal";

export default class SampleCard extends React.Component {
  state = {
    deleteConfirmModal: false
  };

  handleModalToggle = () => {
    this.setState(({ deleteConfirmModal }) => {
      console.log('deleteConfirmModal = ', deleteConfirmModal);
      return {
        deleteConfirmModal: !deleteConfirmModal
      };
    });
  };

  render() {
    console.log('this.state.deleteConfirmModal = ', this.state.deleteConfirmModal);
    return (
      <React.Fragment>
        <button onClick={() => this.handleModalToggle()}>Delete Modal</button>
        <DeleteModal displayed={this.state.deleteConfirmModal} />
      </React.Fragment>
    );
  }
}

删除模式:

import React from 'react';

export default class DeleteModal extends React.Component {
constructor(props){
  super(props);
  this.displayed = this.props.displayed;
}
  render() {
    console.log('props = ', this.props);
    // You can manage the style through a class of course, would be better
    return (
        <div className="modal" style={{display: this.props.displayed ? 'block':'none'}}>
          Are you sure you want to delete the item?
        </div>
    );
  }
}

重现模态的小css:

html, body, #root{
  margin: 0;
  padding: 0;
  position: relative;
}

.modal{
  position: absolute;
  width: 300px;
  height: 300px;
  top: 50px;
  left: 50px;
  border: 1px solid black;
  border-radius: 4px;
}

【讨论】:

  • 我在我的实际代码中导出,在这里我粘贴了代码,只是为了让我了解我想要做什么
  • 好的,但是你如何显示/隐藏你的模态?这里没有什么可以处理这个任务。你能展示你如何调用你的&lt;DeleteModal /&gt; 组件以及你如何处理它的显示吗?我已经编辑了我的代码,以向您展示我将如何做这种事情。
  • 我可以编辑我的问题并给你看吗?我也合并了您的更改,但仍然无法从 SampleCard 组件触发模式
  • 是的,请编辑您的问题,以便每个人都可以看到它:)
猜你喜欢
  • 2020-09-11
  • 2021-07-09
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 2022-11-24
相关资源
最近更新 更多