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

useFieldArray: mutators always take name from closure of the first render. #171

Closed
ddgrishkin opened this issue Oct 13, 2022 · 1 comment

Comments

@ddgrishkin
Copy link
Contributor

ddgrishkin commented Oct 13, 2022

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

When I update a first argument name of the hook useFieldArray, methods push and remove work incorrectly (suspect that others work the same way): they always reference to the value of argument name that was provided into useFieldArray at the first render. That's why elements aren't added and removed for an actual value of the name.

What is the expected behavior?

Elements should be added and removed from actual value of the argument name.

Sandbox Link

https://codesandbox.io/s/react-final-form-arrays-report-qjlneq

Other information

That's happening because mutators are returned by the useConstant hook only at the first render, and variable name is always taken from a closure of the first call.

const mutators = useConstant<Mutators>(() =>
    // curry the field name onto all mutator calls
    Object.keys(formMutators).reduce((result, key) => {
      result[key] = (...args) => formMutators[key](name, ...args)
      return result
    }, {})
  )

const mutators = useConstant<Mutators>(() =>

I could fix it with useMemo. What do you think of it?

@erikras
Copy link
Member

erikras commented Oct 19, 2022

Published fix in v3.1.4.

@erikras erikras closed this as completed Oct 19, 2022
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

2 participants