I am building a todo app and my form has an input element in it where the value is stored in a state variable. The form should submit that value to an array but when I press submit it doesn't show and only shows the value when I try to submit another value., and so on and so on.
import React, { useState } from "react";
function App() {
const [inputValue, setInputValue] = useState("");
const [todos, setTodos] = useState([]);
const handleSubmit = (e) => {
e.preventDefault();
const name = inputValue;
if (name === "") return;
setTodos((prevTodos) => {
return [...prevTodos, { id: name, name: name, isComplete: false }];
});
console.log(todos);
setInputValue("");
};
return (
<main>
<form onSubmit={handleSubmit}>
<input
type="text"
className="todo-input"
placeholder="Create a new todo..."
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
</form>
</main>
);
}
export default App;