Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!