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

113
Views
React update page with input info when pressing a button

react newbie here once again. I'm trying to update the page with a note when pressing 'send' button. Honestly I tried for so long that I think my code is pretty much a mess rn.

Main func:

function MainLogged() {

    const [sendingScreen, setSendingScreen] = useState(true);
    let notes = [];
    const [note, setNote] = useState('')

    function onNoteChange(event){
        const {value} = event.target;
        setNote(value);
    }

    function afterButtonClick(event) {
        notes.push(note);


        setSendingScreen((prev) => !prev)
    }

    return (
    <div id='main'>
        {sendingScreen ? <MainBody noteCallback={onNoteChange} callback={afterButtonClick}/> : <ResetScreen callback={afterButtonClick}/> }
        {console.log(notes)}
        { 
            notes.map((singleNote) => { 
                return (<p id='single-note' className='note'>{singleNote}</p>)
            })
        }
    </div>
 )
}

then goes to component:

function MainBody(props) {

    const {noteCallback, callback} = props;


    return (
        <div>
            <p id='main-body'>{lorem}</p>
            <div className='card text-center' style={{width: '18rem', alignSelf: 'center'}}>
                <Input onChange={noteCallback} type='text' placeholder='My note is...' className='card-body information' />
            </div>
            <SendCardButton callback = {callback}/>
        </div>
    )
}

and finally (if necessary):

function SendCardButton(props){

    const {callback} = props;
    return (
        <button type="button" className="btn btn-success toBeCentered" onClick={callback}>Send</button>
    )
}

Using this code leaves notes array empty always.

Please if you can explain so I can learn and improve, I REALLY need it right now. Thanks a lot!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

React does not rerender your component after you click the button because notes are not stored in state. You need declare notes this way:

const [notes, setNotes] = useState([]);

And then set them on button click, not just push the new one:

setNotes((prevNotes) => ([...prevNotes, note]));

That should be enough:) Feel free to comment if you have further questions

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