简体   繁体   中英

Passing a callback function to a rendered react component

I have a react component that I am calling/rendering on a legacy html page. How do I make react call a javascript function in the legacy html page?

====== contents from html file ======
<script>
       function buttonClicked(){
          console.log('buttonClicked');
       }
</script>
<div id="sample-react-component"> 
</div>

==================================
=======Contents from JSX =========

class Foo extends Component {
  handleClick = () => {

     //
     // how do I call buttonClicked here?
     //
  }
  render() {
    return <button onClick={this.handleClick}>Click Me</button>;
  }
}

const domContainer = document.querySelector('#sample-react-component');
ReactDOM.render(<Foo/>, domContainer);

==================================================

You can call it using window object using window.buttonClicked().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM