简体   繁体   English

如何将数据从 html/JavaScript 页面传递到 React 组件

[英]How do I pass data from html/JavaScript page to a React Component

I have a working react application.我有一个有效的反应应用程序。 If I render the component on the bottom of App.js , it works.如果我在App.js的底部渲染组件,它就可以工作。 It bundles everything together.它将所有东西捆绑在一起。

However, I would like to render the react component in the file that includes bundle.js .但是,我想在包含bundle.js的文件中呈现反应组件。

So on the HTML page I have this code:所以在 HTML 页面上我有这段代码:

<script src="Scripts/ParsedRequests/dist/bundle.js"></script>
<script>
  var props = {
    rowData: { make: 'Toyota2', model: 'Celica', price: 35000 }
  };

  console.log(props);

  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(React.createElement(Main, props, null));
</script>

The error I am getting is: ReactDOM is not defined我收到的错误是: ReactDOM is not defined

You should add the JS libraries in your HTML file, React and ReactDOM , so you can use the functions in your JS code.你应该在你的 HTML 文件中添加 JS 库, ReactReactDOM ,这样你就可以在你的 JS 代码中使用这些函数。

 <div id="root"></div> <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script> <script type="text/babel"> var props = { rowData: { make: 'Toyota2', model: 'Celica', price: 35000 } }; function Main(props) { console.log(props); return ( <div> Make: {props.rowData.make}<br />Model: {props.rowData.model}<br />Price: {props.rowData.price} </div> ); } const container = document.getElementById('root'); const root = ReactDOM.createRoot(container); root.render(React.createElement(Main, props, null)); </script>

You can read more about it on the official doc: https://reactjs.org/docs/add-react-to-a-website.html您可以在官方文档上阅读更多相关信息: https ://reactjs.org/docs/add-react-to-a-website.html

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

相关问题 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何从现有的 web 页面将回调 function 传递给 React 组件? - How do I pass callback function to a React component from an existing web page? 在 React/Next.js 中,如何简单地将数据从一个页面的组件传递到另一个页面上的组件? - How can I simply pass data from a component one page to a component on another page in React/Next.js? 如何将数据从 vanilla JavaScript 传递到 React 功能组件 - How to pass data from vanilla JavaScript to React functional component 如何在React中将单击的组件中的数据从一条路径传递到另一条路径? - How do I pass data from a clicked component from one Route to another in React? 如何在 NuxtJS 中将数据从组件传递到页面? - How do you pass a data from component to page in NuxtJS? 我该如何使用JavaScript将这个bootstrap html表单传递给下一页? - how do i pass this bootstrap html form to next page with javascript? 从 HTML 页面将数据传递到 React 组件? - Passing data into a React component from the HTML page? 如何将数据从 React 组件传递到主文件? - How can I pass data from a React component to main file? 如何使用 React 将数据从列表传递到另一个组件? - How can I pass a data to another component from a list with React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM