Passing function between component

In this article going to show how we can pass function between components.

This is very simple in react

In this example we pass a function App component to Profile component

 

App component

import React from "react";
import Profile from "./compontents/Profile";

export default function App() {

  const sayHello = () => {
    alert("Hi, I am a function from app component")
  }

  return (
    <>
      <Profile sayHello={sayHello}></Profile>
    </>
  );

}

 

Profile component

import React from "react";

export default function Profile({ sayHello}) {

  return (
    <>
      <button onClick={sayHello}>Click here</button>
    </>
  );

}

 

About Author

Photo

Hi, I am Mahbub Hasan
Software Engineer

I am very much interested to share my programming
and development knowledge with the people.I will try
to update my blog everyday with new technology inshallah.

Thank You

For Visiting My Blog.

Contact Me