Open Dylan-Cairns opened 2 years ago
any progress here ?
Does anybody have a fix or a workaround? Or maybe it has been fixed but not released yet?
For our use case it's very easy to avoid this issue, the solution is don't use numeric keys in nested objects.
The main problem is just knowing this behavior exists.
I'm not sure a code change is really required here, but maybe a note should be added to the docs, perhaps the 'nested objects' section under 'arrays'.
Thanks for your answer @Dylan-Cairns
I agree that should be at least documented but I think, with my little Formik experience, as it is working as expected on values
it can/should work perfectly on errors
& touched
too...
Any solutions here? It's annoying when I have to use numeric keys.
Any solutions here? It's annoying when I have to use numeric keys.
The only solution for now is to add a string prefix to number keys.
values = {
someKey: "value1",
nestedObject: { "f3": "value for 3" }
}
Any solutions here? It's annoying when I have to use numeric keys.
The only solution for now is to add a string prefix to numeric keys.
values = { someKey: "value1", nestedObject: { "f3": "value for 3" } }
I find it's OK to use number keys since it make no difference when getting error/touched from formik.errors/touched, you can just use formik.errors.nestedObject[numeric_key]
as the same as string keys.
And for formik.values, there is also no differece since all keys will be converted to string when posting JSON data to backend.
I find it's OK to use number keys since it make no difference when getting error/touched from formik.errors/touched, you can just use
formik.errors.nestedObject[numeric_key]
as the same as string keys. And for formik.values, there is also no differece since all keys will be converted to string when posting JSON data to backend.
If all those undefined
in the array doesn't mess your stuff I agree its fine.
Also to mention for small numeric key you dont fell the difference, but lets say the key is 94788
, what will be the length of the nestedObject
array ? I would say minimum 94788
if it is not the max key. So iterating over big array with undefined to just show to errors looks a little messy.
If all those
undefined
in the array doesn't mess your stuff I agree its fine. Also to mention for small numeric key you dont fell the difference, but lets say the key is94788
, what will be the length of thenestedObject
array ? I would say minimum94788
if it is not the max key. So iterating over big array with undefined to just show to errors looks a little messy.
The array is a sparse array, means the 'undefined' keys are actually 'empty', no memory cost, and will be skipped when map or loop.
If all those
undefined
in the array doesn't mess your stuff I agree its fine. Also to mention for small numeric key you dont fell the difference, but lets say the key is94788
, what will be the length of thenestedObject
array ? I would say minimum94788
if it is not the max key. So iterating over big array with undefined to just show to errors looks a little messy.The array is a sparse array, means the 'undefined' keys are actually 'empty', no memory cost, and will be skipped when map or loop.
Are you sure about that? I've just spent hours investigating an "out of memory" bug (visible in the browser console) in a Formik form of a proprietary React application. The bug indeed came from the numeric IDs used as field names, but since these IDs were very high values (> 1E9), this caused the bug described in this ticket. Adding a string prefix to the IDs works well as a solution.
Are you sure about that? I've just spent hours investigating an "out of memory" bug (visible in the browser console) in a Formik form of a proprietary React application. The bug indeed came from the numeric IDs used as field names, but since these IDs were very high values (> 1E9), this caused the bug described in this ticket. Adding a string prefix to the IDs works well as a solution.
You can confirm by console.log(array)
to see if it has empty
in output, not null
or undefined
.
Bug report
When the values object for a form contains a nested object, if one of those object properties (ie a field name in the form) is numeric, then when onBlur or onChange is triggered, the nested object will be replaced with an array. A value will be set for the element of the array at the index matching the integer which is a property name. In the touched object, the value 'true' will be assigned to the element at that index. In the errors object, the error text will be assigned.
For example, if the field name is '3', the nested object in 'touched' will be converted to an otherwise empty array with the value 'true' at index 3.
Current Behavior
As described above. In the above example the shape would look like:
Expected behavior
The nested object should remain an object. The object property at the matching key should be updated appropriately based on onBlur/onChange.
In the example above, the correct shape of touched should be
Reproducible example
This sandbox has a form with 3 fields. The first 2 field names are numbers (1 and 5 respectively) and the 3rd is a string.
Code Sandbox
Your environment