'Why the the input length is 1?
The user has the possibility to generate input fields in order to enter a value, but what I want is the input length to be at 0.
const [list, setList] = useState([{ name: "" }]);
const handleClick = () => {
setList([...list, { name: "" }]);
};
console.log(inputList) // in the console it shows the length 1
const handleItemChanged = (event, index) => {
const value = event.target.value;
const newlist = [...list];
newlist[index].items = value;
list(newlist);
};
So when the user goes to this component, it is shown an input field, and that's because the length as initial is 1.
How can I make it the length 0?
Solution 1:[1]
At the time of declairation you already set a item. So if you want to the list length will be 0, at initial then do it as blank aray. like
const [inputList, setInputList] = useState([]);
Solution 2:[2]
Because you always have [{items:''}] at least in your array, which is array with one entry, therefore length 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chinmay Dey |
| Solution 2 | Wraithy |
