我有一个react组件,我正在调用/渲染一个遗留的html页面。我如何使react调用javascript函数在遗留的html页面?
====== 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);
==================================================
您可以使用window
对象调用window.buttonClicked()。