繁体   English   中英

带有属性名称的 useFieldArray 的打字稿问题

[英]Typescript problem with useFieldArray with property name

我对 useFieldArray 有以下打字稿问题。 它在名称下的控制器无法识别: name={test.${index}.firstname}

That's where the error comes: controller.d.ts(18, 5): The expected type comes from property 'name' which is declared here on type 'IntrinsicAttributes & { render: ({ field, fieldState, formState, }: { field: ControllerRenderProps<Record<string, any>, ${string}|${string}.${string}|${string}.${number}>; fieldState: ControllerFieldState; formState: FormState<...>; }) => ReactElement<...>; } & UseControllerProps<...>'

然后在 defaultValue 下, item.name 和 item.value 都没有这些子项在 item 下不存在。

我怎么解决这个问题?

我的代码: https : //codesandbox.io/s/stoic-frost-lpnwe?file=/src/components/BasicData.tsx

这是官方文档中的解决方案。

https://react-hook-form.com/api/usefieldarray/

在打字稿部分:

对于嵌套字段数组,您必须按名称强制转换字段数组。

const { fields } = useFieldArray({ name: `test.${index}.keyValue` as 'test.0.keyValue' });

所以对于你的情况,名字应该是

name={`test.${index}.firstname` as `test.0.firstname`}

这是演示的代码和框

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM