skip to Main Content

What Is wrong With My code cause form state always back to initial value or it always create new instance of "forms" class


interface loginComponentProps {}

const LoginComponent: FC<loginComponentProps> = () => {
 
 const [loggedIn, setloggedIn] = useState(false);
 const [form, setFormInstance] = useState(new forms({ userName: '', password: '' } as LoginDto,
 [
 { Key: 'userName', validationFunc: Validations.isRequird },
 { Key: 'password', validationFunc: Validations.isRequird },
 ]))
..........
 return (
 <ImageBackground/>  
     ..........
 </ImageBackground>
 );
};

2

Answers


  1. Your codes seem correct. Check your form component and make sure onChange methods of your form children components work correctly.

    Login or Signup to reply.
  2. It’s difficult to see without a more of your code shown (how is LoginComponent consumed, how are the login form acting, etc), but the short version is your state hooks are being reset every render, which is why form keeps being reset.

    Either provide some persistence for the current state of form, falling back to a new forms(...) if no data is persisted, or ensure that your hooks to handle form management correctly use things like useCallback with the correct dependency arrays.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search