编程语言
首页 > 编程语言> > javascript – 在React Styled Components上使用’ref’不起作用

javascript – 在React Styled Components上使用’ref’不起作用

作者:互联网

我在使用样式组件的refs时遇到了困难.当我尝试在我的类方法中访问它们时,我收到以下错误:

Edit.js:42 Uncaught TypeError: this…..contains is not a function

  constructor(props) {
    ....
    this.setWrapperRef = this.setWrapperRef.bind(this);
    this.handleClickOutside = this.handleClickOutside.bind(this);
   }
----------
  setWrapperRef = (node) => {
    this.wrapperRef = node;
  }
  handleEdit = (e) => {
    e.preventDefault();
    this.props.onEdit(this.props.id, this.state.title);
  }
----------
<Wrapper onSubmit={this.handleEdit} ref={this.setWrapperRef}>
  ...
</Wrapper>

我找到了this question的代码

我在这做错了什么?

解决方法:

我自己找到了答案.解决方案是使用innerRef而不是ref,因为ref本身指向Styled Component而不是DOM节点.

详细讨论可以在GitHub找到

标签:ref,javascript,dom,reactjs,styled-components
来源: https://codeday.me/bug/20190910/1800126.html