美文网首页Web 前端开发 react
记一笔之:react 在 componentDidMount 中

记一笔之:react 在 componentDidMount 中

作者: 文艺的小唐 | 来源:发表于2017-06-17 14:45 被阅读0次

今天遇到一个诡异的情况,有一个表单验证的问题,使用了 ref 获取 input 的引用:

this.myInputRefs = {}     // 初始化refs,节点如下
<MyInput ref = {el => this.myInputRefs.usernameRef = el} />

然后在 componentDidMount 中获取:

componentDidMount() {
    console.log(this.myInputRefs)    
}

以上打印出来,结果居然是:{usernameRef: null} !
暂时没有去深究原因,于是换了一种写法就搞定了:

<MyInput ref='usernameRef' />

然后:

this.myInputRefs.usernameRef = this.refs.usernameRef

就这样能正常获取到

相关文章

网友评论

    本文标题:记一笔之:react 在 componentDidMount 中

    本文链接:https://www.haomeiwen.com/subject/nqtbqxtx.html