简体   繁体   English

ReactJS class 组件和功能组件如何从普通 Javascript ZC1C425268E68385D1AB50741?

[英]How do I call ReactJS class component and functional component from normal Javascript function?

I wanted to call a ReactJS function ( functional Component ) from the normal JavaScript function.我想从普通的 JavaScript ZC1C4145268E178A8 中调用 ReactJS function(功能组件)。 Its not printing the output because ReactJS uses the JSX syntax.它不打印 output 因为 ReactJS 使用 JSX 语法。 Is it possible to call ReactJS functional component from the normal JS fucntion?是否可以从正常的 JS 功能调用 ReactJS 功能组件? My Example code is here:我的示例代码在这里:

class Extra extends React.Component { 
      //This is the ReactJS class component 
}

function Greeting() {
       // This is the ReactJS functional component
      // I wanted to call this ReactJS functional component from the 
      normal JavaScript function 

      return (<Extra/>);
}

function Calling() {
    // This  is normal JavaScript function  which I have defined in the 
     separate file. From this function I wanted to call the ReactJS 
     function.

     Greeting();
}

I tried this.我试过这个。 I called ReactJS functional component from the normal JS function.我从普通的JS function中调用了ReactJS功能组件。 It is going inside the ReactJS function but its not displaying the JSX code on the browser ( JSX sytax of the ReactJS Class component) It is going inside the ReactJS function but its not displaying the JSX code on the browser ( JSX sytax of the ReactJS Class component)

Thanks in advance提前致谢

Yes you can call Component class function from normal javascript function, but you need to make that react function static then call from noraml javascript by className.react_function() Yes you can call Component class function from normal javascript function, but you need to make that react function static then call from noraml javascript by className.react_function()

class Extra extends React.Component { 
  //This is the ReactJS class component 

  static Greeting=()=>{
    console.log("Greeting from react class function");
  }
}


function Calling() {
  // This  is normal JavaScript function  which I have defined in the  separate file.From this function I wanted to call the ReactJS function.
 Extra.Greeting();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM