Simulate click event on react element

后端 未结 7 1982
孤独总比滥情好
孤独总比滥情好 2020-12-03 03:06

I\'m trying to simulate a .click() event on a React element but I can\'t figure out why it is not working (It\'s not reacting when I\'m firing the

7条回答
  •  天涯浪人
    2020-12-03 03:52

    Inspired from previous solution and using some javascript code injection it is also possibile to first inject React into the page, and then to fire a click event on that page elements.

    let injc=(src,cbk) => { let script = document.createElement('script');script.src = src;document.getElementsByTagName('head')[0].appendChild(script);script.onload=()=>cbk() }
    injc("https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js",() => injc("https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js",() => {
    
    class ReactInjected extends React.Component{
      simulateClick(e) {
        e.click()
      }
      render(){
        return 
    console.log('click injection')}> hello
    } } ReactDOM.render(, document.getElementById('app')) } ))

提交回复
热议问题