gpt4 book ai didi

reactjs - React hook 形式 useController typescript 类型错误

转载 作者:行者123 更新时间:2023-12-05 04:39:49 26 4
gpt4 key购买 nike

我试图拆分此示例中的代码: https://codesandbox.io/s/usecontroller-0o8px

但是当我通过 control={control} 时出现类型错误输入组件。

(JSX attribute) control?: Control<FieldValues, object> | undefined
Type 'Control<formValues, object>' is not assignable to type 'Control<FieldValues, object>'.
The types of '_options.resolver' are incompatible between these types.
Type 'Resolver<formValues, object> | undefined' is not assignable to type 'Resolver<FieldValues, object> | undefined'.
Type 'Resolver<formValues, object>' is not assignable to type 'Resolver<FieldValues, object>'.ts(2322)
controller.d.ts(22, 5): The expected type comes from property 'control' which is declared here on type 'IntrinsicAttributes & { label?: string | undefined; supportive?: string | undefined; } & UseControllerProps<FieldValues, string> & InputHTMLAttributes<...> & { ...; }'

这是我的输入组件

type Props = {
label?: string
supportive?: string
} & UseControllerProps &
InputHTMLAttributes<HTMLInputElement>

const InputComp: FC<Props> = ({ label, supportive, ...props }) => {

...

const {
field,
fieldState: { invalid, isTouched, isDirty },
formState: { touchedFields, dirtyFields },
} = useController(props)

return <input {...field} />
}

当我将 formValues 类型传递给 UseControllerProps<formValues> 时错误消失了。

我想以不同的形式使用输入组件 formValues我怎样才能做到这一点?

最佳答案

这是一个对我有用的解决方案。我用了this以及指向原始代码片段的链接以获得帮助。

我有一个可重用的 Headless UI ListBox 组件,我想将下拉值与我的原始表单界面一起传递给它。

React Hook Form 起源引用:

enum State {
AL = "AL",
...
}

export interface UserRegistration {
firstName: string
lastName: string
streetAddress: string
streetAddress2: string
city: string
state: State
zipCode: string
}

const registration = () => {

const {
control
} = useForm<UserRegistration>()

return (
...

<Dropdown
control={control}
name="state"
dropdownOptions={State}
/>
)
}

可重用组件:

import { Fragment } from 'react'
import { Listbox, Transition } from '@headlessui/react'
import { useController, UseControllerProps } from 'react-hook-form'

interface DropdownProps {
dropdownOptions: any
}

const Dropdown = (props: DropdownProps & UseControllerProps<UserRegistration>) => {
const {
field: { value, onChange },
} = useController(props)

return (
<div>
<Listbox value={value} onChange={onChange}>
<div className="relative">
<Listbox.Button>
<span>{value}</span>
<span>
<SelectorIcon aria-hidden="true" />
</span>
</Listbox.Button>
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Listbox.Options>
{Object.keys(props.dropdownOptions).map((value, index) => (
<Listbox.Option
key={index}
value={value}
>
{({ selected }) => (
<>
<span>
{value}
</span>
{selected ? (
<span>
<CheckIcon aria-hidden="true" />
</span>
) : null}
</>
)}
</Listbox.Option>
))}
</Listbox.Options>
</Transition>
</div>
</Listbox>
</div>
)
}

关于reactjs - React hook 形式 useController typescript 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70392508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com