Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
how to call a function contained within a direct child component from the parent? (functional components)

I am new to react. Basically I want that when I click on the button of the child component, the function myFunction is called from the parent component. How can I do it?

This is my live code

import "./styles.css";
export const Parent = ({ children }) => {
  //I need excecute myFunction when user click
  return (
    <div>
      I am parent <br />
      <br /> {children}
    </div>
  );
};
export const Child = () => {
  const myFunction = () => {
    console.log("say hello from parent");
  };
  return (
    <div>
      I am children
      <br />
      <button>Send function to execute in the parent</button>
    </div>
  );
};

export default function App() {
  return (
    <Parent>
      <Child />
    </Parent>
  );
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try calling child inside parent function as shown below :

import "./styles.css";

export const Child = (props) => {
  const myFunction = () => {
    console.log("say hello from parent");
  };
  return (
    <div>
      I am children
      <br />
      <button onClick={props.randomFun}  >Send function to execute in the parent</button>
    </div>
  );
};

export const Parent = () => {
  //I need excecute myFunction when user click
  const handleChild=()=>{console.log("You clicked on child button")}
  return (
    <div>
      I am parent <br />
      <br /> 
      <child randomFun = {handleChild} />
    </div>
  );
};

export default function App() {
  return (
    <Parent />
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

We can use useRef,

Here we have a parent component with a button and a child component with a function to show an alert. If you want to call the showAlert function when the button is clicked, there is no direct way to access it.

import { forwardRef, useRef, useImperativeHandle } from "react"

const ChildComp = forwardRef((props, ref) => {
  useImperativeHandle(ref, () => ({
    showAlert() {
      alert("Hello from Child Component")
    },
  }))
  return <div></div>
})

function App() {
  const childCompRef = useRef()
  return (
    <div>
      <button onClick={() => childCompRef.current.showAlert()}>Click Me</button>
      <ChildComp ref={childCompRef} />
    </div>
  )
}

export default App
about 4 years ago · Juan Pablo Isaza Report

0

you want this?

export const Child = () => {
   const myFunction = () => {
    console.log("say hello from parent");
  };
  return (
    <div>
      I am children
      <br />
      <button onClick={()=>myFunction()}>Send function to execute in the parent</button>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!