Skip to content

Commit ac9f23e

Browse files
committed
Added faq about #103
1 parent a19235a commit ac9f23e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/faq.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [Via `document.getElementById()`](#via-documentgetelementbyid)
1313
* [Via Closure](#via-closure)
1414
* [Via Redux Dead Drop](#via-redux-dead-drop)
15+
* [Why can't I have numeric keys in an object?](#why-cant-i-have-numeric-keys-in-an-object)
1516

1617
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1718

@@ -108,3 +109,29 @@ See [Sandbox Example](https://codesandbox.io/s/1y7noyrlmq).
108109
If you're already using Redux, you could potentially use the same mechanism that
109110
`redux-form` uses:
110111
[Redux Dead Drop](https://medium.com/@erikras/redux-dead-drop-1b9573705bec).
112+
113+
## Why can't I have numeric keys in an object?
114+
115+
So you want to have value structured like `{ 13: 'Bad Luck', 42: 'Meaning of Everything' }`, but you're getting an error. This is because the `setIn` engine in 🏁 Final Form uses the `isNaN`-ness of keys to determine whether or not it should create an object or an array when constructing deep data structures. Adding checks for `bracket[3]` syntax as opposed to `dot.3` syntax adds a _lot_ of complexity, and has consciously been avoided.
116+
117+
You will need to convert all your keys to `NaN` strings before initializing your form values, and then convert them back to numbers on submit. It's not that hard.
118+
119+
```jsx
120+
const stringifyKeys = values =>
121+
Object.keys(values).reduce((result, key) => {
122+
result[`key${key}`] = values[key]
123+
return result
124+
}, {})
125+
126+
const destringifyKeys = values =>
127+
Object.keys(values).reduce((result, key) => {
128+
result[Number(key.substring(3))] = values[key]
129+
return result
130+
}, {})
131+
132+
<Form
133+
onSubmit={values => onSubmit(destringifyKeys(values))}
134+
initialValues={stringifyKeys(initialValues)}>
135+
...
136+
</Form>
137+
```

0 commit comments

Comments
 (0)