I know this is completely a noob question and I'm sorry but I try to figure out how should I manage to put the Object in the state array when submitting the form. Thank you
interface newList {
name: string;
}
const ListAdder = () => {
const [listName, setListName] = useState("");
const [listArray, setListArray] = useState<any>([]);
const submitHandler = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const myNewList: newList = {
name: listName,
};
setListArray([...listArray].push(myNewList));
setListName("");
console.log(listArray);
};
const listNameHandler = (e: FormEvent<HTMLInputElement>) => {
setListName(e.currentTarget.value);
};
return (
<>
<form onSubmit={submitHandler}>
<label>Create your List</label>
<br />
<input
type="text"
placeholder="List name is..."
value={listName}
onChange={listNameHandler}
/>
<button type="submit">Add the List</button>
</form>
</>
);
};
setListArray([...listArray].push(myNewList));tosetListArray([...listArray, myNewList]);