[英]Can not assign new property in react , got ` Can't add property xxx, object is not extensible`
I want to make a dialog component used to submit form. 我想制作一个用于提交表单的对话框组件。
class SubmitDialog extends React.PureComponent {
toggleDialog() {
this.setState({isOpen:!this.state.isOpen})
}
handleSubmit() {
// children need have handleSubmit
this.props.children.props.handleSubmit()
this.toggleDialog()
}
render(){
const {children, title } = this.props;
// I want to pass a button to trigger open this dialog
let trigger = React.cloneElement(this.props.trigger);
trigger.onClick = this.toggleDialog;
return (
{trigger},
<Dialog
open={this.state.isOpen}
onClose={() => {this.toggleDialog()}}
className="dialog"
>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>
<DialogBody scrollable className="dialog-body">
{children}
</DialogBody>
<DialogFooter className="align-axis">
<Button raised className="cyan white-text" onClick={()=> { this.toggleDialog() }}>取消</Button>
<Button raised primary onClick={()=> { this.handleSubmit() }} >提交</Button>
</DialogFooter>
</Dialog>
)
}
};
I want to pass 我想通过
Because outside component don't know how to open this dialog, I tend to pass the element into dialog, and let dialog pass a onClick funtion to it. 因为外部组件不知道如何打开此对话框,所以我倾向于将元素传递给对话框,然后让对话框将onClick函数传递给它。 But I found I can't add set property to this.props.trigger
. 但是我发现我无法将set属性添加到this.props.trigger
。 I tried to add React.cloneElement
, but still not work. 我试图添加React.cloneElement
,但仍然无法正常工作。
try: 尝试:
React.cloneElement(this.props.trigger, {
onClick: this.toggleDialog
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.