When I press "Submit" button the console shows "success" even though the input is empty, and when I press again it finally shows "error".
How do I change the code so that console will show "error" when I press the button for the first time?
import React, { useState } from "react";
import "./App.css";
function App() {
const [input, setInput] = useState("");
const [error, setError] = useState(false);
const submitHandler = (e) => {
e.preventDefault();
if (input === "") {
setError(true);
}
if (error) {
console.log("error");
return;
}
console.log("success");
};
return (
<div className="App">
<form onSubmit={submitHandler}>
<input onChange={(e) => setInput(e.target.value)}></input>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default App;