-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Make fields of useFieldArray return an array of key-value pairs #8197
Make fields of useFieldArray return an array of key-value pairs #8197
Conversation
You know, it actually makes a lot of sense. Now, I haven't thought of all the use-cases yet, but I like it |
Thanks for the feedback and address that in a PR. I am happy to change the key from 'id' to 'key' which was already something we already kind addressed in v7 because it overlaps with the user data field id. however, I am not too keen to introduce another level of { value, key } as it's already working with v8, which no longer needs to message with the keyName https://codesandbox.io/s/sweet-thompson-cvg6od?file=/src/App.js let me know if the above codesandbox works for your use-case, if so, feel free to adjust your PR to change |
Basically |
src/useFieldArray.ts
Outdated
@@ -382,8 +382,8 @@ export function useFieldArray< | |||
fields: React.useMemo( | |||
() => | |||
fields.map((field, index) => ({ | |||
...field, | |||
id: ids.current[index] || generateId(), | |||
value: field, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer to avoid this breaking change.
Yes, it works when you bind it to an input with |
By the way, I would be happy only with the |
I am happy to proceed with the above, and the other use case you pointed out above, you can get that from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🥇
@@ -30,7 +30,7 @@ | |||
"build:modern": "rollup -c ./scripts/rollup/rollup.config.js", | |||
"build:esm": "rollup -c ./scripts/rollup/rollup.esm.config.js", | |||
"prettier:fix": "prettier --config .prettierrc --write \"**/*.{ts,tsx,css}\"", | |||
"lint": "eslint '**/*.{js,ts,tsx}'", | |||
"lint": "eslint \"**/*.{js,ts,tsx}\"", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old one was not working for me. Maybe it is specific to Windows?
I know this is a long shot, but I thought it would be more convincing if I opened a PR. This is the PR for my comment here
Basically, I think the
fields
array should return the data without a loss. Previously, we could change thekeyName
option to some key we know that does not exist in our original object. But in the v8, there won't be akeyName
option and it will be alwaysid
, which conflicts with an existing property very often. There is a plan to change it tokey
, but that could also conflict for some people & projects.I suggest we separate the key and value prop to clear this confusion once and for all. I made alternative suggestions in the discussion. I would also be willing to help in that direction if that is what's agreed upon.
This is the gist of the change, and the rest is changes to tests.
https://github.com/react-hook-form/react-hook-form/compare/react-18...KurtGokhan:v8-field-array-key-value-pair?expand=1#diff-ee762f75b15dde4fde77049f36e387fa0421f37dc1c29dad72fceba8a23c0e4dR25
The number of changes to tests gives an idea of how much of a breaking change this is. Typescript will catch on missing
id
property and will warn the users. However it could be problematic if people don't use Typescript and they are unaware of this change.