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

177
Vistas
Understanding the useState and input logic

im having trouble to get my UseState to show itself once i click a button, currently i have a function, that that use usestate and show my value on the screen, for example, when i write "miki" in the input box, it will show "miki" on the screen, however, it happens when i click the input box, and i want it to happen when im pressing the button, this is my code untill now:

import react, { useState } from "react";

export const ShoppingListPageContainer = () => {
    const [name, setName] = useState(``)
    const nameChange = (event) => {
        setName(event.target.value)
    }

    return <div>
        <input label="name: " id="name" onClick={nameChange} />
        <input type="number" name="quantity" />
        <button> Add </button>
        <p> {name} </p>
    </div>
}   

as u can see, the onClick is on the input and thats why it works when pressing the inputbox, but, when i switch the onClick location to the button, it shows nothing, because there arent any value inserted to the button, obviously, any help?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are using the onClick property (in onClick={nameChange}). This means your nameChange function gets called every time a user clicks into the input box.

You should change this to onChange={nameChange} and add the value={name} to the input:

<input label="name: " id="name" onChange={nameChange} value={name} />

This means that the nameChange function gets called every time the input changes. The value will also be set to name, so if anything else calls nameChange, it will be reflected in the input box.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use the onChange event in the input field. And use the useEffect hook and its dependency to get the current data

useEffect(() => {
  console.log('name', name);
}, [name])
<input type="string" onChanhe={(e:any) => nameChange(e.target.value)} />
<p>{name}</p>

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