-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue / Question regarding how useForm initialState is handled #2427
Comments
Call form.resetDirty when your form is submitted – https://codesandbox.io/s/mantine-useform-issue-forked-fcsvl8?file=/src/App.tsx |
Hi @rtivital For me this looks not satisfying. Let me explain more context: Actually your implementation require that I do the following into a useEffect to have the complete useForm state reset :
some thoughts: I understand this break the immutable identity reset fn has now. This is a weakness of React we can patch with using a ref and useLayoutEffect see this Thread if you want to keep the constant identity of reset. |
Just ran into this issue as well. If my form's initialValues are set to props that eventually defined, the initialValues will remain undefined. |
@RedFour Instead of passing any props to initial values, just left them empty, and use Example from: https://mantine.dev/form/recipes/ useEffect(() => {
const storedValue = window.localStorage.getItem('user-form');
if (storedValue) {
try {
form.setValues(JSON.parse(window.localStorage.getItem('user-form')));
} catch (e) {
console.log('Failed to parse stored value');
}
}
}, []); |
What package has an issue
@mantine/form
Describe the bug
I have a component showing 2 informations (They are passed with props). Since it's so small, I decided to incorporate an edit modal and handle the two fields state with
useForm
Hook.useForm
only take in accountinitialValues
at first call, then place the values in a React ref. This leads to a bug after my 2 informations are changed a first time: For the second edition, form will be dirty without changes, and even worse, the dirty flag will be false if you bring back the values which were present BEFORE the first Edition.I wrote a reproduction app on CodeSandbox: sandbox here
Reproduction step:
Are initialValues placed into a react ref on purpose? Is there any reason?
What version of @mantine/hooks page do you have in package.json?
5.3.0
If possible, please include a link to a codesandbox with the reproduced problem
https://codesandbox.io/s/mantine-useform-issue-z4350f?file=/src/App.tsx
Do you know how to fix the issue
Yes
Are you willing to participate in fixing this issue and create a pull request with the fix
Yes
Possible fix
As we have to make a deep equal comparison between actual state and initial state I wonder why we can't update the initialState if the value passed to the hook call changes? Whats the interest of placing it into a react ref?
The text was updated successfully, but these errors were encountered: