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

93
Views
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 answers
Answer question

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