简体   繁体   English

在纯 js 文件中调用 React class

[英]Calling a React class in a pure js file

I have a pure javascript file from which Im getting all the inputs, it's a legacy code.我有一个纯 javascript 文件,我从中获取所有输入,这是一个遗留代码。 Now I want a react component to get called from the javascript file现在我想从 javascript 文件中调用一个反应组件

App(exercise, event); //Something like this, App is my React class. //像这样,App是我的React class。 exercise and event are inputs from js file练习和事件是来自 js 文件的输入

So in my case as the inputs are getting stored in the js file from various dropdowns, the js file is the entry point of my application.因此,在我的情况下,由于输入从各种下拉列表中存储在 js 文件中,因此 js 文件是我的应用程序的入口点。

import React from 'react';

export default class App extends Component{
  render(){
    return(<div>
      {console.log(this.props)}
    </div>)
  }
}

How should my React class be imported in js file so it can be called and I can see the exercise and event in props of react我的 React class 应该如何导入到 js 文件中以便可以调用它,我可以在 react 的 props 中看到练习和事件

Currently Im getting Uncaught Reference Error: App is not defined in the console output目前我得到Uncaught Reference Error: App is not defined

Your js code can mount the component and pass in the initial properties to it.您的 js 代码可以挂载组件并将初始属性传递给它。

ReactDOM.render(React.createElement(App, {...properties}), document.getElementById('target'))

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

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