javascript - 我如何通过外部javascript调用用reactjs编写的函数?

标签 javascript html reactjs

假设我在渲染之前在 index.js 文件中用 reactjs 编写了一个测试函数

function testfunction(){
console.log("this is test function written in reactjs")
}

我想在 html 脚本中调用上面的 reactjs 函数。

<html>
<body>

//including reactjs minified js in the index.html
<script src="reactjs.min.js"></script>

<script>
//here i want to call function written in reactjs file
testfunction();
</script>

</body>
</html>

最佳答案

这是丑陋且糟糕的做法,但您可以将函数设置为全局(窗口)上下文,以便其他脚本可以调用它:

window.testfunction = () => console.log("this is test function written in reactjs");

然后,页面上有权访问 window 的其他脚本将能够调用它。

关于javascript - 我如何通过外部javascript调用用reactjs编写的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376636/

相关文章:

javascript - 在新选项卡中打开后更改 html 链接的文本颜色?

javascript - 如何获取内部函数的返回值

javascript - fitBounds 未正确居中

javascript - Font Awesome 堆叠图标悬停轮廓问题

python - 在 Flask 中打印元组列表

javascript - React - 作为导致额外渲染的 Prop

reactjs - 在react-redux应用程序中需要什么mapDispatchToProps?

javascript - nodejs 与多个子进程通信,无法识别它们

php - zend 和安全

javascript - 将 react-dropzone 与 nextjs 一起使用 - 输入类型为 "file"的多个属性在服务器渲染时卡在 false 上,如何将其设置为 true?