this.refs.something returns “undefined”

前端 未结 6 1692
野趣味
野趣味 2020-12-08 03:52

I have an element with a ref that is defined and ends up getting rendered into the page :

    
... <
6条回答
  •  难免孤独
    2020-12-08 04:54

    Check that you are not accessing ref before the child component has been mounted. E.g. it doesn't work in componentWillMount. A different pattern which auto invokes ref related callback after the element has been mounted is this-

    (console.log(elem))}/>

    You can use this notation to get mounted elements in deep nesting as well -

提交回复
热议问题