![](/img/trans.png)
[英]"Property 'name' does not exist on type" typescript problem in react website
[英]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.