This is my code for signup page:
import { useState } from "react";
import { Link } from "react-router-dom";
export default function SignUp() {
const [formData, setformData] = useState({});
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const handleChange = (e) => {
setformData({ ...formData, [e.target.id]: e.target.value });
};
// console.log(formData);
const handleSubmit = async (e) => {
e.preventDefault();
try {
setLoading(true);
const res = await fetch("/api/auth/signup", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
});
const data = await res.json();
if (data.success === false) {
setError(data.message);
setLoading(false);
// setError(null);
}
setLoading(false);
console.log(data);
} catch (error) {
setLoading(false);
setError(error.message);
}
return (
<div className="p-3 max-w-lg mx-auto">
<h1 className="text-3xl text-center font-semibold my my-7">Sign Up</h1>
<form className="flex flex-col gap-4" onSubmit={handleSubmit}>
<input
type="text"
placeholder="Username"
className="border p-3 rounded-lg"
id="username"
onChange={handleChange}
/>
<input
type="text"
placeholder="Email"
className="border p-3 rounded-lg"
id="email"
onChange={handleChange}
/>
<input
type="password"
placeholder="Password"
className="border p-3 rounded-lg"
id="password"
onChange={handleChange}
/>
<button
disabled={loading}
className="bg-slate-700 text-white p-3 rounded-lg uppercase hover:opacity-90 disabled:opacity-70"
>
{loading ? "Loading..." : "Sign Up"}
</button>
</form>
<div className="flex gap-2 mt-5">
<p>Have an account?</p>
<Link to={"/sign-in"}>
<span className="text-blue-700">Sign in</span>
</Link>
</div>
{error && <p className="text-red-500 mt-5">{error}</p>}
</div>
);
};
}
whats wrong with this?
I was just trying to add ToastContainer to show some error messages but as i saw it stopped rendering the signup form i undid all the steps using ctrl+z. But still it didnt render my form on the page.
The form was rendering just fine before all this hazard.
There is no error in console and all the other links are working fine.
2
Answers
I think there’s a mistake somewhere, F12 to check it out.
You have closing bracket
}
at the wrong place —handleSubmit
isn’t closed properly. Instead, it includes the entirereturn
block.The fix is to simply move the
}
to the line aftersetError(error.message);
Heres the complete fixed code