React is primarily used as a JavaScript library and is commonly integrated into web applications using JavaScript bundlers like Webpack or tools like Create React App. However, it is also possible to include React directly in HTML without using a bundler, although it is not the recommended approach for most projects.
To include React directly in HTML, you need to include the React library and the ReactDOM library, which is used for rendering React components into the DOM. You can include them by adding the following script tags in the HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>React in HTML</title>
</head>
<body>
<div id="root"></div>
<!-- Include React library -->
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<!-- Include ReactDOM library -->
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<!-- Your React component code -->
<script>
// Define your React component
const App = () => {
return React.createElement('h1', null, 'Hello, React in HTML!');
};
// Render the component into the DOM
ReactDOM.render(React.createElement(App), document.getElementById('root'));
</script>
</body>
</html>
In the example above, the React and ReactDOM libraries are included via script tags from the unpkg CDN. You can use the React.createElement
function to create React elements, and the ReactDOM.render
function to render the React component into the DOM.
While this approach works for simple examples, it becomes less practical for larger applications, as managing dependencies, transpiling JSX, and handling complex project structures are better handled by using a build tool like Webpack or Create React App. These tools provide a more efficient and maintainable development environment for React projects.