- React - Home
- React - Introduction
- React - Roadmap
- React - Installation
- React - Features
- React - Advantages & Disadvantages
- React - Architecture
- React - Creating a React Application
- React - JSX
- React - Components
- React - Nested Components
- React - Using Newly Created Components
- React - Component Collection
- React - Styling
- React - Properties (props)
- React - Creating Components using Properties
- React - props Validation
- React - Constructor
- React - Component Life Cycle
- React - Event management
- React - Creating an Event−Aware Component
- React - Introduce Events in Expense Manager APP
- React - State Management
- React - State Management API
- React - Stateless Component
- React - State Management Using React Hooks
- React - Component Life Cycle Using React Hooks
- React - Layout Component
- React - Pagination
- React - Material UI
- React - Http Server
- React - Http client programming
- React - Form Programming
- React - Forms
- React - Controlled Component
- React - Uncontrolled Component
- React - Formik
- React - Conditional Rendering
- React - Lists
- React - Keys
- React - Routing
- React - Redux
- React - Animation
- React - Bootstrap
- React - Map
- React - Table
- React - Managing State Using Flux
- React - Testing
- React - CLI Commands
- React - Building and Deployment
- React - Example
- Hooks
- React - Introduction to Hooks
- React - Using useState
- React - Using useEffect
- React - Using useContext
- React - Using useRef
- React - Using useReducer
- React - Using useCallback
- React - Using useMemo
- React - Custom Hooks
- React Advanced
- React - Accessibility
- React - Code Splitting
- React - Context
- React - Error Boundaries
- React - Forwarding Refs
- React - Fragments
- React - Higher Order Components
- React - Integrating With Other Libraries
- React - Optimizing Performance
- React - Profiler API
- React - Portals
- React - React Without ES6 ECMAScript
- React - React Without JSX
- React - Reconciliation
- React - Refs and the DOM
- React - Render Props
- React - Static Type Checking
- React - Strict Mode
- React - Web Components
- Additional Concepts
- React - Date Picker
- React - Helmet
- React - Inline Style
- React - PropTypes
- React - BrowserRouter
- React - DOM
- React - Carousel
- React - Icons
- React - Form Components
- React - Reference API
- React Useful Resources
- React - Quick Guide
- React - Cheatsheet
- React - Axios CheatSheet
- React - Useful Resources
- React - Discussion
React - Using CDN
Let us learn how to use content delivery network to include React in a simple web page. Starting from React 19 encourages usage of CDN which supports ES Modules directly. esm.sh is one such CDN. Such CDN allows to use import/export statements in javascript.
<script type="module">
import React from 'https://esm.sh/react';
import ReactDOM from 'https://esm.sh/react-dom/client';
</script>
Open a terminal and go to your workspace.
cd /go/to/your/workspace
Next, create a folder, static_site and change directory to newly created folder.
mkdir static_site cd static_site
Next, create a new HTML file, hello.html.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple React app</title>
</head>
<body>
</body>
</html>
Next, include React library.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple React app</title>
</head>
<body>
<script type="module">
import React from 'https://esm.sh/react';
import ReactDOM from 'https://esm.sh/react-dom/client';
</script>
</body>
</html>
Here,
We are using unpkg CDN. unpkg is an open source, global content delivery network supporting npm packages.
@17represent the version of the React library
This is the development version of the React library with debugging option. To deploy the application in the production environment, use below scripts.
<script src="https://unpkg.com/react@17/umd/react.production.min.js" crossorigin></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" crossorigin></script>
Now, we are ready to use React library in our webpage.
Next, introduce a div tag with id react-app.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React based application</title>
</head>
<body>
<div id="react-app"></div>
</body>
</html>
The react-app is a placeholder container and React will work inside the container. We can use any name for the placeholder container relevant to our application.
Next, create a script section at the end of the document and use React feature to create an element.
<!DOCTYPE html>
<html>
<head>
<title>React based application</title>
</head>
<body>
<div id="react-app"></div>
<script type="module">
import React from 'https://esm.sh/react';
import ReactDOM from 'https://esm.sh/react-dom/client';
function App() {
return React.createElement('h1', null, 'Hello React!');
}
const root = ReactDOM.createRoot(document.getElementById('react-app'));
root.render(React.createElement(App));
</script>
</body>
</html>
Here, the application uses React.createElement and createRoot.render methods provided by React Library to dynamically create a HTML element and place it inside the react-app section.
Next, serve the application using serve web server.
serve ./hello.html
Next, open the browser and enter http://localhost:3000 in the address bar and press enter. serve application will serve our webpage as shown below.
We can use the same steps to use React in the existing website as well. This method is very easy to use and consume React library. It can be used to do simple to moderate feature in a website. It can be used in new as well as existing application along with other libraries. This method is suitable for static website with few dynamic section like contact form, simple payment option, etc., To create advanced single page application (SPA), we need to use React tools. Let us learn how to create a SPA using React tools in upcoming chapter.