从react组件调用外部Javascript函数 [英] Call external Javascript function from react components

查看:348
本文介绍了从react组件调用外部Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是之前曾被问过的还是有人在reactjs上遇到过相同的问题.所以情况是这样的,我有一个 index.html 文件,其中包含一些javascript.现在,在我的react组件上,我有一个条件,该条件仅在条件为true时才会呈现.这意味着,最初在我的页面加载时,尚未呈现该组件.当我切换按钮时,这就是渲染该组件的地方.该子组件需要调用index.html中包含的javascript方法.我该怎么办?

Im not sure if this has been asked before or anybody has encountered the same issue on reactjs. So the scenario is like this, I have an index.html file that includes some javascript. Now on my react component, I have a condition that will render only if the condition is true. This means that initially when my page loaded the component has not been rendered yet. When I toggle a button this is where that component gets rendered. That child component needs to call a javascript method that was included on my index.html. How should I do this?

非常感谢您的帮助.

推荐答案

在index.html

In index.html

<script type="text/javascript">
  function test(){
    alert('Function from index.html');
  }
</script>

在您的组件中

componentWillMount() {
  window.test();
}

这篇关于从react组件调用外部Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆