如何从外部JS文件中调用反应js中的函数

时间:2023-01-26 13:23:52

I was trying to create a pop over for one of my elements which are using react and bootstrap.

我试图为我的一个使用react和bootstrap的元素创建一个pop。

Following is my React Code:-

以下是我的反应代码: -

class Share extends React.Component{
   render(){
      return(
        <div>hello</div>
      );
    }
  }

  var shareRenderFunc=function() {
     ReactDOM.render(
      <Share/> , document.getElementById('shareContainer')
  );
 }

Following is my jquery called the shareRenderFunc() :-

以下是我的jquery,名为shareRenderFunc(): -

var popOverSettings = {
placement: 'bottom',
container: 'body',
selector: '[rel="popover"]', 
content: function(){
  window.shareRenderFunc();
  return $('#shareContainer').html();
  }
}

$('body').popover(popOverSettings);

Following is my HTML containing the popover element:-

以下是包含popover元素的HTML: -

<div className="like-share icons-gray-black">
      <a href=""><span><i className="fa fa-heart" aria-hidden="true"></i></span></a>
      <a><span><i className="share-social fa fa-share-alt" rel="popover" aria-hidden="true"></i></span></a>
</div>

But in the console I am getting TypeError saying shareRenderFunc is not a function

但是在控制台中我得到TypeError,说shareRenderFunc不是一个函数

P.S: the reactjs file is loaded before the jquery file so the function called is already there, i tried same by creating another javascript file and calling function present in there and it seems to be working fine.

P.S:reactjs文件在jquery文件之前加载,所以调用的函数已经存在,我通过创建另一个javascript文件并调用函数存在于那里尝试相同,它似乎工作正常。

2 个解决方案

#1


4  

Your shareRenderFunc only exists in the local scope of your Share class. You need to define it as global via the window object :

您的shareRenderFunc仅存在于Share类的本地范围内。您需要通过window对象将其定义为全局:

class Share extends React.Component{
   render(){
      return(
        <div>hello</div>
      );
    }
 }

window.shareRenderFunc=function() {
     ReactDOM.render(
         <Share/> , document.getElementById('shareContainer')
     )
);

#2


0  

did u try to change

你试图改变吗?

var shareRenderFunc=function() {
     ReactDOM.render(
      <Share/> , document.getElementById('shareContainer')
     );
}

in to this...

在这......

function shareRenderFunc(){
    ReactDOM.render(
        <Share/> , document.getElementById('shareContainer')
    );
}

you only need to call the function wihtout params if you didnt' set one.

如果你没有设置一个,你只需要调用params的函数。

shareRenderFunc();

PS: If you want your jQuery load before your reactsjs, then try to write your jQuery script over your reactsjs line

PS:如果你想在你的reactsjs之前加载你的jQuery,那么尝试在你的reactsjs行上编写你的jQuery脚本

<script src="jquery ...."></script>
/* and here your reactsjs */

#1


4  

Your shareRenderFunc only exists in the local scope of your Share class. You need to define it as global via the window object :

您的shareRenderFunc仅存在于Share类的本地范围内。您需要通过window对象将其定义为全局:

class Share extends React.Component{
   render(){
      return(
        <div>hello</div>
      );
    }
 }

window.shareRenderFunc=function() {
     ReactDOM.render(
         <Share/> , document.getElementById('shareContainer')
     )
);

#2


0  

did u try to change

你试图改变吗?

var shareRenderFunc=function() {
     ReactDOM.render(
      <Share/> , document.getElementById('shareContainer')
     );
}

in to this...

在这......

function shareRenderFunc(){
    ReactDOM.render(
        <Share/> , document.getElementById('shareContainer')
    );
}

you only need to call the function wihtout params if you didnt' set one.

如果你没有设置一个,你只需要调用params的函数。

shareRenderFunc();

PS: If you want your jQuery load before your reactsjs, then try to write your jQuery script over your reactsjs line

PS:如果你想在你的reactsjs之前加载你的jQuery,那么尝试在你的reactsjs行上编写你的jQuery脚本

<script src="jquery ...."></script>
/* and here your reactsjs */