Skip to content
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

Using initialValues with nested object causes infinite re-renders #51

Open
bhuvanmehra opened this issue Jun 27, 2019 · 4 comments
Open

Comments

@bhuvanmehra
Copy link

bhuvanmehra commented Jun 27, 2019

Are you submitting a bug report or a feature request?

Bug

What is the current behavior?

Using a nested JSON object in initialValues causes infinite re-renders.
Also, form fields can't be changed.

Steps-

  1. Open the code sandbox link
  2. Try to change the first name and last name fields
  3. Open console to see the infinite re-render console log messages

Example -

const Test = () => {
  const { form, handleSubmit, values, pristine, submitting } = useForm({
    onSubmit,
    validate,
    initialValues: {
      name: {
        firstName: 'Clark',
        lastName: 'Kent',
      }
    }
  });

What is the expected behavior?

Form should render once with initial values.
And we should be able to update form field values.

Sandbox Link

https://codesandbox.io/s/cold-https-6v26o

What's your environment?

node: v10.2.1
react-final-form-hooks: "2.0.0",
Mac OS Mojave 10.14.2

Other information

Screenshot
Screen Shot 2019-06-27 at 5 35 16 pm

@linyanm
Copy link

linyanm commented Jul 28, 2019

I think that we could initialize value after react rendering, that means should set initial value on useEffect, in this case, a temporary solution is:

useEffect(() => {
  form.initialize({
    name: {
      firstName: 'Clark',
      lastName: 'Kent',
    }
  });
}, []);

@bhuvanmehra
Copy link
Author

Thanks @l1nyanm1ng

@Jephuff
Copy link
Contributor

Jephuff commented Feb 4, 2020

For future visitors to this issue, you can also use useMemo to make sure the object reference stays the same

const initialValues = useMemo(() => ({
  name: {
      firstName: 'Clark',
      lastName: 'Kent',
    }
}), [])
const Test = () => {
  const { form, handleSubmit, values, pristine, submitting } = useForm({
    initialValues
  });
}

@bhuvanmehra
Copy link
Author

Thanks @Jephuff for the work around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants