【发布时间】:2020-06-03 05:26:48
【问题描述】:
所以我在 React 中使用 ref 时遇到了一点麻烦。
我要做的就是使用这样的 ref 打印元素的文本内容:
export default class SomeClass extends Component {
constructor(props) {
super(props);
this.intro = React.createRef();
console.log(this.intro.textContent);
}
render() {
return (
<div ref={this.intro}>Hi</div>
)
}
}
但是,这总是打印 null 或 undefined 而不是我想要的“Hi”。
【问题讨论】: