Experiment 10: ReactJS – Render HTML, JSX, Components – function & Class

 Write a program to render HTML to a web page.



Create a React Application

Open Command Prompt / Terminal and run:


npx create-react-app myapp


cd myapp


npm start


Browser opens automatically at:
http://localhost:3000

React is successfully installed!


myapp

 ├── node_modules

 ├── public

 │    └── index.html

 ├── src

 │    ├── App.js

 │    ├── index.js

 │    └── App.css

 └── package.json


index.html



<!DOCTYPE html>

<html>

<head>

  <title>ReactJS – Render HTML</title>


  <!-- React CDN -->

  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>

  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>


  <!-- Babel -->

  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

</head>


<body>

  <h2>ReactJS Experiment 10</h2>


  <!-- Root Element -->

  <div id="root"></div>


  <!-- JS files -->

  <script type="text/babel" src="App.js"></script>

  <script type="text/babel" src="index.js"></script>


</body>

</html>


index.js


// Rendering App component into root element

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(<App />);


App.js


// Function Component

function App() {

  return (

    <div>

      <h1>Welcome to ReactJS</h1>

      <p>This content is rendered using JSX.</p>

      <ul>

        <li>HTML Rendering</li>

        <li>JSX Syntax</li>

        <li>React Components</li>

      </ul>

    </div>

  );

}



Write a program for creating and nesting components (function and class). 


App.js


import React from "react";


/* ---------- Function Component ---------- */

function Header() {

  return (

    <div>

      <h1>Welcome to React</h1>

      <p>This is a Function Component</p>

    </div>

  );

}


/* ---------- Class Component ---------- */

class Content extends React.Component {

  render() {

    return (

      <div>

        <h2>React Components</h2>

        <p>This is a Class Component</p>


        {/* Nesting a Function Component inside Class Component */}

        <Header />

      </div>

    );

  }

}


/* ---------- Parent Function Component ---------- */

function App() {

  return (

    <div>

      <h1>Main App Component</h1>


      {/* Nesting Class Component inside Function Component */}

      <Content />

    </div>

  );

}


export default App;


********************

App (Function Component)
 └── Content (Class Component)
      └── Header (Function Component)


my-react-app
 ├── public
 │    └── index.html
 └── src
      ├── index.js
      └── App.js

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>React Nested Components</title>
</head>
<body>
  <!-- React mounts here -->
  <div id="root"></div>
</body>
</html>


index.js

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);

Comments

Popular posts from this blog

Experiment 1: Node.js

Experiment 6: ExpressJS – Routing, HTTP Methods, Middleware