Hi I have a small React app that can increase by 1 when user click on a button. User can also type in the value they want in the input box. However, when I type in a value and then click the button, the value is treated a string not a number i.e. 4 -> 41 -> 411 instead of 5 and 6. This works fine if I click the button without type in the value i.e. 0->1. Does anyone know why this happens ? Thanks
export default function App() {
const [value, setValue] = React.useState(0);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<div>
<input
value={value}
onChange={(event) => {
setValue(event.target.value); // -> still a number
console.log(typeof value);
}}
/>
<button
onClick={() => {
console.log(typeof value); // -> it is a string
setValue(value + 1);
}}
>
Click me
</button>
</div>
</div>
);
}