简体   繁体   English

如何调用react js函数或传递参数以从普通JS / HTML响应js?

[英]How to call react js function or pass arguments to react js from normal JS/HTML?

I am developing a web app where I need to do certain things in React JS. 我正在开发一个Web应用程序,我需要在React JS中做某些事情。 I have written the react js code. 我已经写了react js代码。

But I want it to be called only when a button is clicked. 但我希望仅在单击按钮时才能调用它。 How to do this? 这个怎么做?

I read the tutorial but still couldn't figure out this. 我阅读了本教程,但仍然无法弄清楚。

Please help me. 请帮我。 Struck for 2 hours. 打了两个小时。

If you wish to render a <ReactComponent/> at <div id="mountPoint"></div> using a button click and wish to pass an Object data to React, this should solve your problem. 如果您希望通过单击按钮在<div id="mountPoint"></div> <ReactComponent/>处呈现<ReactComponent/>并希望将对象data传递给React,这应该可以解决您的问题。

The button below is not rendered using React. 以下按钮未使用React呈现。 There must also be a mount point specified beforehand where React will mount the <ReactComponent/> : 还必须事先指定一个安装点,React将在该安装点安装<ReactComponent/>

<button onclick=ReactRender()> Render React </button>
<div id="mountPoint"></div>

The button calls a function ReactRender() which does all the rendering. 该按钮调用一个函数ReactRender()来完成所有渲染。

//Now to render React
var ReactRender = function(){
  // data is an Object which you wish to pass to React
  ReactDOM.render(<ReactComponent data={data}></ReactComponent>, mountPoint);
}

The data you have passed from outside React will be available as this.props.data inside ReactComponent . 您从React外部传递的data将在this.props.data内部以ReactComponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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