79413839

Date: 2025-02-05 06:40:53
Score: 0.5
Natty:
Report link

You can easily access the input field value in the button’s onClick event. Just give an ID to the input field and get its value using document.getElementById. Here's a simple example:

import { useState } from 'react';

export default function App() {
  const [num, setNum] = useState(0);

  function newNum(value) {
    setNum(value);
  }

  return (
    <>
      <h1>Changing Number Using useState</h1>
      <h2>Your number is {num}</h2>
      <input placeholder="Write new number here" id="numid" />
      <button
        onClick={() => {
          const num = document.getElementById('numid');
          newNum(num.value);
        }}
      >
        Change
      </button>
    </>
  );
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Kishan