Adding react to html
تبليغيرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.
I managed to understand how to add React to HTML, but faced a question. In tutorials I saw React is adding directly to HTML with 3 react scripts in head. In other I saw react is adding to JS file with import React from ‘react’ etc. When I connect js file in HTML, I write <script src=”script.js” type=”text/babel”></script> But if I didn’t add those 3 scripts in head, but have import in js, nothing works.
So, question is, how to add JS file with React with imports in js file, if it’s possible.
Doesn’t work with started >npx serve
Example of code that doesn’t work:
rh.html:
<!DOCTYPE html>
<html>
<body>
<div id=”root”></div>
<script src=”rh.js” type=”text/babel”></script>
</body>
</html>
rh.js:
import React from ‘react’;
import ReactDOM from ‘react-dom/client’;
const element = (
<table>
<tr>
<th>Name</th>
</tr>
<tr>
<th>Age</th>
</tr>
<tr>
<th>Region</th>
</tr>
</table>
)
ReactDOM.render(element, document.getElementById(‘root’));
أضف إجابة