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>
</>
);
}