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

95
Vistas
Get the input value in React hooks and print it

I am trying to print the value of input on the screen, but it keeps updating every time I enter a new character.

/*Import*/
import React, { useState } from "react";
import "./Search.scss";
/*Component*/
const Search = () => {
  /*State*/
  const [input, setInput] = useState("");
  /*On Form Submit*/

  return (
    <>
      <div className="Search">
        <form onSubmit={(e) => e.preventDefault()} className="Search__form">
          <input
            value={input}
            onChange={(e) => setInput(e.target.value)}
            type="text"
            placeholder="&#xF002; Title, companies, expertise, or benefits"
            style={{ fontFamily: "Arial, FontAwesome" }}
          ></input>
          <button onclick={console.log(input)}>Search</button>
        </form>
        <h1>{input}</h1>
      </div>
    </>
  );
};

/*Exprting*/
export default Search;

I just want to print the whole value after I click the button

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

0

You are calling console.log(input) on each rerender instead of only on click. Convert it to an arrow function and it should do what you want.

<button onClick={()=>console.log(input)}>Search</button>

EDIT: To answer your follow-up question: You need two separate state variables:

/*Import*/
import React, { useState } from "react";
import "./Search.scss";
/*Component*/
const Search = () => {
  /*State*/
  const [input, setInput] = useState("");
  // new state variable
  const [submittedInput, setSubmittedInput] = useState("");
  /*On Form Submit*/

  return (
    <>
      <div className="Search">
        <form onSubmit={(e) => e.preventDefault()} className="Search__form">
          <input
            value={input}
            onChange={(e) => setInput(e.target.value)}
            type="text"
            placeholder="&#xF002; Title, companies, expertise, or benefits"
            style={{ fontFamily: "Arial, FontAwesome" }}
          ></input>
          <button onClick={()=>setSubmittedInput(input)}>Search</button>
        </form>
        <h1>{submittedInput}</h1>
      </div>
    </>
  );
};
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