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
Your codes seem correct. Check your form component and make sure onChange methods of your form children components work correctly.
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 whyform
keeps being reset.Either provide some persistence for the current state of
form
, falling back to anew forms(...)
if no data is persisted, or ensure that your hooks to handle form management correctly use things likeuseCallback
with the correct dependency arrays.