今天遇到一个诡异的情况,有一个表单验证的问题,使用了 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
就这样能正常获取到
网友评论