react-hook-form: Unexpected fields values with useFieldArray when setting defaultValues
Describe the bug I’m not 100% sure that this is a bug, but I know that what is happening is not what I expected.
When setting defaultValues on an array with useForm, I would expect the result fields array using useFieldArray to contain those values as-is rather than becoming mangled.
Example:
// In our case, the default values match a GraphQL schema so
// we can't necessarily change the shape
const { control } = useForm({
defaultValues: {
sources: ['abc.com', 'def.com']
}
});
const { fields } = useFieldArray({ control, name: 'sources' });
console.log('fields', fields);
// Expected output
/**
[{
"value": "abc.com",
"id": "5f0137bf-e201-425e-a7b0-f6cf76e0b3ca"
},
{
"value": "def.com",
"id": "5a93295c-7d9e-4762-a003-52406a611882"
}]
*/
// Actual output:
/**
[{
"0": "a",
"1": "b",
"2": "c",
"3": ".",
"4": "c",
"5": "o",
"6": "m",
"id": "5f0137bf-e201-425e-a7b0-f6cf76e0b3ca"
},
{
"0": "d",
"1": "e",
"2": "f",
"3": ".",
"4": "c",
"5": "o",
"6": "m",
"id": "5a93295c-7d9e-4762-a003-52406a611882"
}]
*/
This issue then snowballs into other issues when trying to display, append, delete, etc the values of the fields, but I figured I’d start here and step through the other issues only if needed.
To Reproduce Steps to reproduce the behavior:
- Go to https://codesandbox.io/s/upbeat-bose-pt26m
- Look at console
- See issue described above
Codesandbox link https://codesandbox.io/s/upbeat-bose-pt26m
Expected behavior
See Expected output in the code snippet above.
Desktop (please complete the following information):
- OS: Windows 10
- Browser: Chrome
- Version: 79.0.3945.130
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 18 (16 by maintainers)
Commits related to this issue
- #924 experience imporvement over watch with field array — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
- 🤞 #924 improvement over watch with field array (#930) * #924 experience imporvement over watch with field array * working in progress * fix empty value situation * improve type * update ... — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
- fix #924: when calling appendId only spread object values, assign other values (e.g. strings, arrays) to value property — committed to ryanwalters/react-hook-form by deleted user 4 years ago
Are you open to supporting strings and arrays as well as objects? It seems like a useful feature to me (obviously 😄). We can probably work out an API that is not a breaking change.
The change I proposed above would be a breaking change, but seems cleaner to me. A non-breaking solution would be to set a
valueproperty only when the incomingvalueis not an object. I’m happy to open a PR for either solution, just let me know.thanks, i added some feedback take a look there 😃
Ah ok, I only included
watchto compare the values side by side. The values inside offieldsare what I was trying to point out. I removedwatchfrom the codesandbox example. It’s possible I’m misunderstanding something, in which case I wouldn’t mind seeing that updated documentation. 😄