我可能在这里做了一些非常愚蠢的事情,但对于我的生活,我无法弄清楚为什么ref总是空的.
import React, { PureComponent } from 'react';
class MyComponent extends PureComponent {
constructor(props) {
super(props);
this.myRef = React.createRef();
console.log(this.myRef);
}
componentDidMount() {
console.log(this.myRef);
}
componentWillUnmount() {
console.log('unmounting');
}
render() {
console.log(this.myRef);
return (
<div ref={this.myRef}>
foobar
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
安慰:
{current: null}
{current: null}
{current: null}
Run Code Online (Sandbox Code Playgroud)
我在React 16.3上.我错过了什么?
| 归档时间: |
|
| 查看次数: |
4037 次 |
| 最近记录: |