Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How to call a function in another component from a different component in react js?

I having two files header.js and upload.js. In upload.js I have a popup modal which should be open when I click upload button in the header.js

So basically I want to call setOpened function present in upload.js from header.js

header.js

import React from 'react'

const Header = () => {
  return (
    <div>
          <Link
             data-bs-toggle="modal"
             onClick={() => setOpened(true)}
             role="button"
            >Upload</Link>
    </div>
  )
}

export default Header

upload.js

import React from 'react'
const [opened, setOpened] = useState(false);

const Upload = () => {
  return (
    <div>
      <Modal
        opened={opened}
        onClose={() => setOpened(false)}
        title="Introduce yourself!"
      >
        {/* Modal content */}
      </Modal>
    </div>
  )
}

export default Upload

In App.js

import Header from './component/Header';
import Footer from './component/Footer';
import Upload from './component/Upload';

function App() {
  return (
   <Header/>
       <Routes>
         <Route path="/upload" element={<Upload/>}/>
       </Routes>
   <Footer/>
);
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To echo @I-vasilich-I's comment about lifting state up, you should move your modal open state to a higher level (like in App) and pass that state to each child that needs it.

For example

const Header = ({ openModal }) => (
  <div>
    <Link data-bs-toggle="modal" onClick={openModal} role="button">
      Upload
    </Link>
  </div>
);

const Upload = ({ opened, closeModal }) => (
  <div>
    <Modal opened={opened} onClose={closeModal} title="Introduce yourself!">
      {/* Modal content */}
    </Modal>
  </div>
);

const App = () => {
  const [opened, setOpened] = useState(false);
  return (
    <>
      <Header openModal={() => setOpened(true)} />
      <Routes>
        <Route path="/upload" element={<Upload opened={opened} closeModal={() => setOpened(false)} />} />
      </Routes>
      <Footer />
    </>
  );
};

(note make sure you add imports)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda