最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a function in react js from an external JS File - Stack Overflow

programmeradmin4浏览0评论

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

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() :-

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:-

<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

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.

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

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() :-

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:-

<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

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.

Share Improve this question asked Jun 8, 2016 at 6:37 Harkirat SalujaHarkirat Saluja 8,1245 gold badges52 silver badges74 bronze badges 2
  • No idea, it should work. What is the type of shareRenderFunc? console.log(typeof window.shareRenderFunc); – vijayst Commented Jun 8, 2016 at 8:35
  • How are you including the React code in your HTML? Are you using webpack, or via babel? As your code is JSX, it'd need to be transpiled into Javascript. The transpilation must be kicking in late, after your jQuery code invokes the function. – hazardous Commented Jun 8, 2016 at 8:42
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

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

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.

shareRenderFunc();

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

<script src="jquery ...."></script>
/* and here your reactsjs */
发布评论

评论列表(0)

  1. 暂无评论