我的模态:
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,共2个,'(属性:只读):“
答案 0 :(得分:1)
在尝试重现它时,我注意到您错过了模态组件上的export
关键字。
您在deleteConfirmModEl
=> deleteConfirmModAl
上也有错字。
修复它并简化您的代码后,它对我有用。这是the repro on Stackblitz,如果它不是ork,则代码为:
样本卡:
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>
);
}
}
DeleteModal:
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;
}