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

69
Visualizações
React pass state between components and pages

I am new to React and very amateur with js. I am having an issue with passing state around components. I have a component that is a button in a separate file. When the user clicks it, they get prompted to do some stuff from a 3rd party and I get back some things(information) like an acct number.

I want to pass that acct number back to app.js and then to other components and pages so that I can change behavior based on it. Below is the code for reference:

app.js

function App() {
    const [mmAccount, setMMAccount] = useState('');

    function handleChildClick(mmAccount) {
        setMMAccount(mmAccount)
        console.log('new acct:', mmAccount);
}


  return (
    <Router>
   
    <SpecialButton mmAccount={mmAccount} onAcctChange={handleChildClick}/>

specialbutton.js

function SpecialButton({ mmAccount, onAcctChange }) {
    
    const { activate, deactivate, account } = imported3rdpartystuff()   

    function handleClick(event) {
    onAcctChange(event.target.account)
    console.log("handle click", mmAccount); 
  }

   if (window.somestuff is true) {
    return [  <p> {!account && <LoadingButton
   } />} onClick={(e) => { activate(); handleClick(e)}}> Connect Here</LoadingButton>}
      {account && <Button
      variant="contained" />}
      onClick={deactivate}>Disconnect: {account.substring(0,5)} ... {account.slice(-4)} </Button>}</p>,
    ];}

I feel like event.target.account is not the way to get the value of account into mmAccount and pass back up to app.js. I've tried maybe 5 different variations of functions and constants. Now, trying explicitly to pass account to this event.target syntax,I don't really understand.

If I pass account directly, I get the following error:

function handleClick(event) {
    onAcctChange(account)

and console.log(mmAccount) in either app.js or the button file, I get value as undefined.

Is the update running before mmAccount gets updated or anyone have another idea of where I went wrong?

about 4 years ago · Juan Pablo Isaza
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