Document.getElementById ('id') in componentDidMount is null

problem description

in componentDidMount, document.getElementById ("id") is null

the environmental background of the problems and what methods you have tried

I try to use ref, to find undefined. in this.refs

related codes

componentDidMount() {
    const fullscreenBtn = document.getElementById("button");
    if (fullscreenBtn) {
      fullscreenBtn.addEventListener("click", () => {
        if (fullscreenBtn.requestFullscreen) {
          fullscreenBtn.requestFullscreen();
        } else if ( document.getElementById("video").msRequestFullscreen) {
          fullscreenBtn.msRequestFullscreen();
        } else if ( document.getElementById("video").mozRequestFullScreen) {
          fullscreenBtn.mozRequestFullScreen();
        } else if ( document.getElementById("video").webkitRequestFullscreen) {
          document.getElementById("video").webkitRequestFullscreen();
        }
      });  
}
}
<div 
  className = "play_img" 
  id="button"
  ref="button" 
  ref={(ref) => this.button = ref}
  onClick={handleClick}>
  <img src={require("./assets/play.png")}  />
</div> 


do not manipulate dom nodes in react . Do not manipulate the dom node in react . Do not manipulate the dom node in react .
if you remember this sentence, there will be no problem with what you said.


onClick event is written on the attribute. Find the element with e.target


if you want to operate the real dom, through refs in componentDidMount, you only need to write the code to get the refs in setTimeout, for example:

setTimeout(() => {
    console.log(this.refs.xxx);
});
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b33b4c-2b21b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b33b4c-2b21b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?