skip to Main Content
function Text() {
  const [text, setText] = useState("");
  return (
    <div className="white">

input from which I need to catch information

      <input type="text" placeholder="enter" onChange={(e) => setText(e.target.value)} />
      <button onClick={console.log(text)}>CLICK</button>

      <div>{text}</div>
    </div>
  );
}

2

Answers


  1. You have to use arrow function in button:

    <button onClick={() => console.log(text)}>CLICK</button>
    
    Login or Signup to reply.
  2. when you put the function like this in onClick :

    <button onClick={console.log(text)}>CLICK</button>
    

    This will make it execute each time the component rerender, however :

    <button onClick={() => {console.log(text)}}>CLICK</button>
    

    will make it execute each time the button is clicked

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search