reactjs – React.createRef始终为null
发布时间:2020-12-15 20:40:46 所属栏目:百科 来源:网络整理
导读:我可能在这里做了一些非常愚蠢的事情,但对于我的生活,我无法弄清楚为什么ref总是为空. import React,{ PureComponent } from 'react';class MyComponent extends PureComponent { constructor(props) { super(props); this.myRef = React.createRef(); conso
我可能在这里做了一些非常愚蠢的事情,但对于我的生活,我无法弄清楚为什么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> ); } } 安慰: {current: null} {current: null} {current: null} 我在React 16.3上.我错过了什么?
omg,我当时正在做出反应16.3,但反应是16.2.碰撞反应到16.3修复了这个问题.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |