I try to set a number in an input field and keep it in a state. However when I try to set the state of the array, it doesn't get populated and when I try it with an anonymous function it creates something like
{
"btm": {
"btm": {
"btm": {...},
"top": 100},
"top": 100},
"top": 100
}
Here the code that does this:
const Numbers = (props) => {
const [numbers, setNumbers] = useState({"btm": "1", "top": "100"});
const fromNumber = (n) => {
console.log(numbers);
setNumbers((n) => {
const copy = numbers;
copy["btm"] = n;
return copy;
});
}
return (
<>
<input variant="toolbar-input" defaultValue={numbers["btm"]} onChange={e => fromNumber(e.target.value)} />
</>
);
}