gpt4 book ai didi

javascript - 在 React 样式组件上使用 'ref' 不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 05:06:57 25 4
gpt4 key购买 nike

我在使用带样式组件的 ref 时遇到困难。当我尝试像下面这样在我的类方法中访问它们时,出现以下错误:

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 上找到详细的讨论。

关于javascript - 在 React 样式组件上使用 'ref' 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50820688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com