[英]How To Call A JavaScript Function Returned From NodeJS in ReactJS JSX
I want to make a function that creates a button in the React DOM.我想做一个 function 在 React DOM 中创建一个按钮。
That function should be returned from NodeJS.应该从 NodeJS 返回 function。
NodeJS - localhost:8000/stripe-form returns: NodeJS - localhost:8000/stripe-form 返回:
function myFunction() {
var btn = document.createElement("BUTTON");
btn.innerHTML = "CLICK ME";
document.body.appendChild(btn);
}
In ReactJS:在 ReactJS 中:
function App() {
return (
<div className="App">
<script type="text/javascript" src="http://localhost:8000/stripe-form"></script>
</div>
);
}
How can I do something similar to this in JSX:我怎样才能在 JSX 中做类似的事情:
<button onClick={myFunction} />
Have a look at https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml看看https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
Something like this may do the trick (using https://github.com/axios/axios for data loading).这样的事情可能会奏效(使用https://github.com/axios/axios进行数据加载)。
async function createMarkup() {
const htmlData = await axios.get("http://localhost:8000/stripe-form")
return {__html: htmlData};
}
function MyComponent() {
return <div dangerouslySetInnerHTML={createMarkup()} />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.