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

261
Views
¿Cómo llamar a una función contenida dentro de un componente secundario directo desde el padre? (componentes funcionales)

Soy nuevo en reaccionar. Básicamente, quiero que cuando haga clic en el botón del componente secundario, se llame a la función myFunction desde el componente principal. ¿Cómo puedo hacerlo?

Este es mi código en vivo

 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

Intente llamar al niño dentro de la función principal como se muestra a continuación:

 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

Podemos usar useRef ,

Aquí tenemos un componente principal con un botón y un componente secundario con una función para mostrar una alerta. Si desea llamar a la función showAlert cuando se hace clic en el botón, no hay una forma directa de acceder a ella.

 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

¿Tu quieres esto?

 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!