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

121
Vistas
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 Respuestas
Responde la pregunta

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