I'm trying to show a component and hide another based on a click event. However, I'm getting this error saying there is an infinite loop.
this is my code
const { register, handleSubmit, formState: { errors } } = useForm();
const [showPage , setShowPage] = useState(true);
{showPage &&(
<div>
<FormContent>
<Form
onSubmit={
handleSubmit((data, e) => {
try {
console.log(data);
setShowPage(false);
e.preventDefault();
//submit(data);
} catch (err) {
console.log(err);
}
})}>
</Form>
</FormContent>
</div>
)}
{!showPage &&(
<ForthPage />
)}
and this is the error i'm getting
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
49 | onSubmit={handleSubmit((data, e) => {
50 | try {
51 | console.log(data);
> 52 | setShowPage(false);
| ^ 53 | e.preventDefault();
54 | //submit(data);
55 | } catch (err) {
Can anyone explain what is wrong with my code
submit()referring to?,handleSubmitshould be acallBack function., andshowPage ? <A/>:<B/>should do the job.