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

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