Monday, June 24, 2024

React Application using CDN

Developing a Simple React Application Without Installing Packages

In this post, you will develop a simple React application without installing the react and react-dom packages. Also, you will understand the limitations of this approach.

To develop a simple React application, you typically need to install the react and react-dom packages. These packages are essential for creating and rendering React components. However, it is technically possible to create a simple React application without explicitly installing these packages by using a CDN (Content Delivery Network) to include React in your HTML file. This approach is often used for learning purposes or quick prototyping. Here's how you can do it:

Step-by-Step Guide

  • Create an HTML File: Create an index.html file as the entry point of your application.
  • Include React and ReactDOM via CDN:

Add the following script tags to your HTML file to load React and ReactDOM from a CDN.


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Simple React App</title>
    </head>
    <body>
        <div id="root"></div>
        <!-- Load React and ReactDOM from a CDN -->
        <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
        <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
        <!-- Load Babel for JSX transformation -->
        <script src="https://unpkg.com/@babel/standalone/babel.min.js" crossorigin></script>
        <!-- Your React code -->
        <script type="text/babel">
            // Define a simple React component
            function App() {
                return <h1>Hello, world!</h1>;
            }
            // Render the component to the DOM
            ReactDOM.render(<App />, document.getElementById("root"));
        </script>
    </body>
</html>
        

Notes

  • Paste the above code in an HTML file in VS Code and open it in Live Server.
  • Babel is included to transform JSX into JavaScript. The script type text/babel tells Babel to process the script.
  • While it is possible to create a simple React application without installing the react and react-dom packages by using CDNs, this approach is limited and primarily useful for small projects or learning purposes. For any serious development, it's a must to set up a proper React development environment.
React JS Common packages

No comments:

Post a Comment

Hot Topics