简体   繁体   English

无法在react中分配新属性,得到了“无法添加属性xxx,对象不可扩展”

[英]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. 我想制作一个用于提交表单的对话框组件。

Code

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>
    )
  }

};

Purpose 目的

I want to pass 我想通过

  1. a form with handleSumbit function (children) 具有handleSumbit函数的表单(子级)
  2. a clickable element to open this dialog (trigger) 一个可点击的元素以打开此对话框(触发)

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 ,但仍然无法正常工作。

Tried Object.seal : 尝试过Object.seal 在此处输入图片说明

try: 尝试:

React.cloneElement(this.props.trigger, {
        onClick: this.toggleDialog
});

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

相关问题 React Error(TypeError):无法添加属性上下文,对象不可扩展 - React Error (TypeError): Can't add property context, object is not extensible 无法添加属性_tracking,对象不可扩展 - Can't add property _tracking, object is not extensible 未捕获的TypeError:无法添加属性12,对象不可扩展 - Uncaught TypeError: Can't add property 12, object is not extensible Node.js React:无法添加属性上下文,对象不可扩展 - Node.js React: Can't add property context, object is not extensible React:无法添加属性“X”,对象不可扩展 - React : cannot add property 'X', object is not extensible 无法添加属性 2,对象不可扩展 Array.push - Can not add property 2, object is not extensible Array.push 未捕获的类型错误:无法定义属性“x”:对象不可扩展 - Uncaught TypeError: can't define property "x": Object is not extensible 类型错误:无法定义属性“当前”:Object 不可扩展 - TypeError: can't define property “current”: Object is not extensible React App-TypeError:无法添加属性setState,对象不可扩展 - React App - TypeError: Cannot add property setState, object is not extensible 无法添加属性,在 React 组件中绑定 props 时对象不可扩展 - Cannot add property, object is not extensible when binding props in React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM