将回调函数传递给呈现的反应组件

时间:2021-01-20 14:51:36

标签: javascript reactjs jsx

我有一个在旧版 html 页面上调用/渲染的 React 组件。如何让 React 调用旧版 html 页面中的 javascript 函数?

====== 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);

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

1 个答案:

答案 0 :(得分:0)

您可以使用 window 对象使用 window.buttonClicked() 调用它。