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")
)

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

Always name the .js folder name after the functional component's name.

Last updated

Was this helpful?