ReactJS, find elements by classname in a React Component

后端 未结 3 1427
名媛妹妹
名媛妹妹 2020-12-28 16:56

I\'ve a React component. Some elements will be inserted through the children. Some of these elements will have a specific classname. How can I get a list of these DOM nodes

3条回答
  •  天命终不由人
    2020-12-28 17:30

    You can achieve it, via findDOMNode of react-dom, like below:

    ReactDOM.findDOMNode().getElementsByClassName('snap') // Returns the elements
    

    If you need the count,

    ReactDOM.findDOMNode().getElementsByClassName('snap').length
    

提交回复
热议问题