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

139
Views
Problema en la función ReactJs llamando entre componentes

Problema: He escrito una función llamada " getData() " en "App.js" y puedo llamarla en "User.js". El código está escrito a continuación.

Pero, ¿qué pasa si he escrito esa función "getdata()" en "User.js" y tengo que llamarla en "App.js". ¿Cómo puedo hacer eso? no soy capaz de hacerlo .. por favor guíame.

Aplicación.js

 import User from './User' function App() { function getData() { alert("Hello from app component") } return ( <div className="App"> <User data={getData}/> </div> ); } export default App:

Usuario.js

 function User(props) { return( <div> <h1>User Component</h1> <button onClick={props.data}> Call Function</button> </div> export default User;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aplicación.js

 import React, { useRef } from 'react'; import User from './User'; function App() { const childRef = useRef(); return ( <div className="App"> <User ref={childRef} /> <button onClick={() => childRef.current.getData()}>Call Function</button> </div> ); } export default App;

Usuario.js

 import React, { forwardRef, useImperativeHandle } from 'react'; const User = forwardRef((props, ref) => { useImperativeHandle(ref, () => ({ getData() { alert('Hello from user component'); }, })); return ( <div> <h1>User Component</h1> </div> ); }); export default User;
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!