简体   繁体   English

从外部javascript文件调用React组件

[英]Invoke React component from an external javascript file

I have a react application and I have a bundle.js file generated through webpack which has all the code for that application. 我有一个React应用程序,我有一个通过webpack生成的bundle.js文件,其中包含该应用程序的所有代码。

Now I have a separate dotnet application. 现在,我有一个单独的dotnet应用程序。 I want to render the top level 'App' component of my react application in this dotnet application. 我想在此dotnet应用程序中呈现我的react应用程序的顶级“ App”组件。

How can this be achieved? 如何做到这一点?

If you have access to the markup for the dotnet application you should be able to include the bundle.js file in a <script> tag and mount your react app to a specified element with an id attribute. 如果您有权访问dotnet应用程序的标记,则应该能够在<script>标记中包含bundle.js文件,并将react应用程序安装到具有id属性的指定元素上。

Example JS (in your bundle.js file): JS示例(在bundle.js文件中):

ReactDOM.render(<App />, document.getElementById('divParent'))

Example HTML: HTML示例:

<body>
  <div id='divParent'></div>
  <script src='http://{yourdomain}/bundle.js' />
</body>

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

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