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

164
Vistas
How to call a react component from another component and pass required props

At the moment, I have the following routes in my App.js file:

<Switch>
  <Route exact path="/new-job"
          render={(props) => <NewJob jobName={jobName} setMenuSelection={handleMenuSelection} />}
  />
  <Route exact path="/past-jobs"
          render={(props) => <PastJobs setMenuSelection={handleMenuSelection} />}
 />
</Switch>

Now within my PastJobs component, I have the following button with onClick process:

<Button
    onClick={() => {
      setConfirmDialog({
         isOpen: true,
         title: `Copy Job ${item.id}?`,
         onConfirm: () => { onCopy(item.job_info) }
      })
   }}         
>
Copy
</Button>

that calls the following function:

const onCopy = (job_info) => {
    setConfirmDialog({
        ...confirmDialog,
        isOpen: false
    })
    history.push({
        pathname: '/new-job',
        state: { detail: job_info }
    })
}       

Within my <NewJob /> component, I have now setup the following as I thought I could access the state.detail but unfortunately it's null, i.e.:

import { useLocation } from 'react-router-dom';

function NewJob( { jobName, setMenuSelection } ) {
    
       const { state } = useLocation();
    
       if (typeof state !== 'undefined') {
         const myVal = state.detail
         console.log("myVal", myVal )
       }
    
}

The issue that I am having and unsure how to approach is that within my onCopy function that is called from button onClick, how do I call the the <NewJob /> component whose path is exact path="/new-job" in App.js above and pass in the prop job_info ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Direct calls to components actually does not exist. But what you are looking can be achieved in different ways.

  1. Using state machine with event bus (redux, redux-saga)
  2. Render props https://reactjs.org/docs/render-props.html
  3. Bunch of callbacks drilled via props (HOC's)
  4. Ref's https://reactjs.org/docs/refs-and-the-dom.html

I suggest to read more about them to actually understand if it matches your use case. Anyhow it is great experience to develop your skills also!

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