【发布时间】:2021-05-15 00:14:40
【问题描述】:
我有一个场景,我需要输入数字作为对象的键
(预期输出)
count: {
1: "value",
2: "value"
}
使用反应钩子形式命名约定,我给了
<input type="text" name={"count.5"} ref={register} />
当我取值时,我得到的是一个数组而不是一个对象。
{count: Array(6)}
> count: Array(6)
0: undefined
1: undefined
2: undefined
3: undefined
4: undefined
5: "1"
代码沙盒:https://codesandbox.io/s/upbeat-kalam-q0wwu?file=/src/App.js
【问题讨论】:
-
这是一个已知问题。看这里:spectrum.chat/react-hook-form/help/…
-
好吧,我正在使用一种解决方法,即设置写入
5 并稍后对其进行修剪。它就是这样工作的。
标签: reactjs react-hooks react-hook-form