why I can't set the Data in the first Click I Have to input twice to set the data. Thanks for your help...
export default function App() {
const [title, setTitle] = useState();
const [projectData, setProjectData] = useState([]);
const handleSubmit = (e) => {
e.preventDefault();
setProjectData([...projectData, { projectTitle: title }]);
console.log(projectData);
};
return (
<div className="App">
<input
type="text"
onChange={(e) => {
setTitle(e.target.value);
}}
/>
<button onClick={handleSubmit}>insert</button>
</div>
);
}
console.log()is just outputting the old data assetStateis asyncronous andprojectDatahasn't changed yet.{JSON.stringify(projectData)}.