簡體   English   中英

反應如何設置組件ID以能夠使用document.getElementById()

[英]React how to set component id to be able to use document.getElementById()

我已經閱讀了幾個問題,我認為只需要像下面那樣設置id就可以了:

<MyComponent id="myId"/>

但是,當我在id上調用document.getElementById() ,將得到null並且當我檢查頁面時,該組件具有一個空ID。

我想念什么?

如@Lekhnath所述,您的MyComponent根元素ID可用於訪問組件。 如下所示,您可以使用“ componentId”訪問元素:

class MyComponent extends Component{
  render(){
  return <div id="componentId">
             ...your child component details here
         </div>
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM