Writing First React App
React Hello World App 
Render a single JSX element into React DOM.
import React from "react"
import ReactDOM from "react-dom"
// Render JSX - html like syntax extension that produces React elements
ReactDOM.render(
  <h1>Hello world!</h1>, 
  document.getElementById("root")
)<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>To render multiple JSX element enclose them within a parent div element.
import React from "react"
import ReactDOM from "react-dom"
// Render JSX - html like syntax extension that produces React elements
ReactDOM.render(
  <div>
    <h1>Hello world!</h1>
    <p>This is a paragraph</p>
  </div>, 
  document.getElementById("root")
)<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>We can use Fragment instead of div in cases where too many nested div elements pollute the DOM.
import React, {Fragment} from "react"
function Grandchild() {
    return (
        <Fragment>
            <hr />
            <h3>I'm the Grandchild component</h3>
            <p>I'm also a part of the Grandchild component</p>
        </Fragment>
    )
}
export default Grandchildimport React, {Fragment} from "react"
import Grandchild from "./Grandchild"
function Child() {
    return (
        <Fragment>
            <h1>I'm the Child component</h1>
            <Grandchild />
        </Fragment>
    )
}
export default Childimport React, {Fragment} from "react"
import Child from "./Child"
function App() {
    return (
        <Fragment>
            <Child />
        </Fragment>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>
Last updated
Was this helpful?