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

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

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 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