Functional Components
A Simple Functional Component
Use regular function to write a functional component.
import React from "react"
import ReactDOM from "react-dom"
// Functional Component
function MyInfo(){
  return(
    <div>
      <h1>Mahmud Sajib</h1>
      <p>Hi, I love to write code & drink coffee!</p>
      <ul>
        <li>India</li>
        <li>Nepal</li>
        <li>Srilanka</li>
      </ul>
    </div>
  )
}
// Render JSX Element
ReactDOM.render(
  <MyInfo />,
  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>Move Components into Separate Files
To keep code clean, it's a good practice to move components into their own separate file.
import React from "react"
// Functional Component
function MyInfo(){
  return(
    <div>
      <h1>Mahmud Sajib</h1>
      <p>Hi, I love to write code & drink coffee!</p>
      <ul>
        <li>India</li>
        <li>Nepal</li>
        <li>Srilanka</li>
      </ul>
    </div>
  )
}
// Exporting Our Component
export default MyInfo
import React from "react"
import ReactDOM from "react-dom"
// Importing Our Component
import MyInfo from "./MyInfo"
// Render JSX Element
ReactDOM.render(
  <MyInfo />,
  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?