this.refs.something returns “undefined”

前端 未结 6 1673
野趣味
野趣味 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:40

    Update since React version 16.4

    In your constructor method define your ref like this

    constructor(props) {
      super(props);
      this.russian = React.createRef();
    }
    

    In your render where you are using ref do this.

      
    

    For e.g if you want to have focus when component mounts do this

    componentDidMount() {
      console.log(this.russian); 
      this.russian.current.focus();
     }
    

    Reference Refs Documentation React

提交回复
热议问题