Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

267
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda