我正在尝试使用自定义Material-UI Autocomplete组件并将其连接到react-hook-form.
TLDR:需要使用带有 react-hook-form 控制器的 MUI 自动完成而不需要
defaultValue
我的自定义Autocomplete组件采用具有结构的对象,{_id:'', name: ''}它显示名称并_id在选择选项时返回。该Autocomplete工作得很好。
<Autocomplete
options={options}
getOptionLabel={option => option.name}
getOptionSelected={(option, value) => option._id === value._id}
onChange={(event, newValue, reason) => {
handler(name, reason === 'clear' ? null : newValue._id);
}}
renderInput={params => <TextField {...params} {...inputProps} />}
/>
Run Code Online (Sandbox Code Playgroud)
为了使工作与react-hook-form我设置的setValues是处理程序onChange中Autocomplete,然后手动注册该组件在useEffect如下
useEffect(() => {
register({ name: "country1" });
},[]);
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我想没有useEffect钩子,只是直接以某种方式使用寄存器。
接下来我尝试使用Controller组件 …
我正在使用react-hook-form。我正在尝试使用这个库实现购物车,但在获取数值方面遇到了困难。使用 getValues("count")时,即使设置type="number", name="count"始终返回字符串值的输入标记。
对于name="count"的字符串值,我的自定义函数增量无法按预期工作。当我单击 + 按钮时,它返回并添加了字符串。
import React, { FC, useEffect } from 'react';
import * as Styled from './style';
import { useForm, ErrorMessage} from 'react-hook-form';
import { ItemCountForm } from 'src/models/item-count';
export const CountForm:FC<{
partialData : Omit<ItemCountForm,'count'>
}> = ({
partialData
}) => {
const { register, handleSubmit, errors,getValues, setValue } = useForm<{count:number}>({
defaultValues:{
count: 1}
});
const onSubmit = async(data: {count: number}) => {
console.log('submitted');
const formData: ItemCountForm = { …Run Code Online (Sandbox Code Playgroud) 我们如何知道 form 是否以react-hook-form 被修改。任何人都有想法。我想知道是否有任何值发生更改并将编辑的状态更新为 true。
在我向 useForm({defaultValues:values}) 提供 defaultValue 后。我想在值更新或更改默认值时收到通知。
我已经使用 MUI 和 React Hook Form 在 React 中构建了一个表单。我正在尝试创建一个用作Select Input的自定义TextField元素。我希望它是一个带有Ref道具的不受控制的组件。我试图按照 MUI 和 React Hook Form 文档的建议传递 inputRef 道具,但没有成功。
<TextField
id="id"
name="name"
select
native="true"
className={classes.textField}
label="label"
margin="normal"
variant="outlined"
inputRef={register({ required: "Choose one option" })}
error={!!errors.name}
>
<MenuItem value="">Choose one option</MenuItem>
<MenuItem value="3">03</MenuItem>
<MenuItem value="6">06</MenuItem>
<MenuItem value="9">09</MenuItem>
<MenuItem value="12">12</MenuItem>
<MenuItem value="16">16</MenuItem>
<MenuItem value="18">18</MenuItem>
</TextField>
Run Code Online (Sandbox Code Playgroud)
我发现的一件事是,如果我将本机select与ref 一起使用,它就可以正常工作。
此外,我尝试将inputRef道具更改为SelectProps道具,但它也不起作用。
先感谢您。
我从文档中获取了示例:
import React from "react";
import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const onSubmit = data => console.log(data);
console.log(watch("example"));
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input defaultValue="test" {...register("example")} />
<input type="submit" />
</form>
);
}
Run Code Online (Sandbox Code Playgroud)
但在每次更改或提交时,我都会得到undefined每个字段
我尝试再次安装该库,但没有任何变化,并且到处都未定义......似乎是注册功能的问题。有人遇到同样的问题吗?
我正在尝试React-Hook-form
复选框的简单代码如下:
import React from 'react'
import { useForm } from 'react-hook-form'
export default function App() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm()
const onSubmit = (data: any) => console.log(data)
console.log(errors)
return (
<div className='mx-auto justify-center p-32 flex'>
<form onSubmit={handleSubmit(onSubmit)}>
<div className='p-2'>
<label htmlFor=''>January</label>
<input
type='checkbox'
placeholder='January'
{...register('January', {})}
className='mx-3'
checked
/>
</div>
<div className='p-2'>
<label htmlFor=''>February</label>
<input
type='checkbox'
placeholder='February'
{...register('February', {})}
className='mx-3'
/>
</div>
<input type='submit' />
</form>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
我可以正确提交表单,但我喜欢一月复选框作为复选框开始,但是当我如代码中所示放置“选中”时,我不知何故无法“取消选中”它。
我似乎错过了一些东西,任何帮助将不胜感激。
我对 typecipt 很陌生,并试图制作一个基本的引脚输入页面。我的代码的沙箱链接。虽然它可以工作,但我在第 10 行的 onChange 函数中收到此错误:
类型“Dispatch<SetStateAction<any[]>>”不可分配给类型“(values?: string) => void”。参数“value”和“values”的类型不兼容。类型“string”不可分配给类型“SetStateAction<any[]>”.ts(2322)
我正在导入react-hook-pin-input,但找不到任何在 ts 中使用 onChange 的演示。请帮我重写 onChange ,使 valueE 成为正在输入的值。可以在这里找到react-hook-pin-input的源代码 - https://github.com/elevensky/react-hook-pin-input
typescript reactjs typescript-typings react-hooks react-hook-form
我正在尝试使用 React Hook Form(版本 7.6.6)构建一个表单。我创建了一个 FormInput 组件,如下所示:
const FormInput = ({ name, label }) => {
const { control } = useFormContext();
return (
<Grid item xs={12} sm={6}>
<Controller
control={control}
name={name}
render={({ field }) => {
return <TextField {...field} label={label} fullWidth required />;
}}
/>
</Grid>
);
Run Code Online (Sandbox Code Playgroud)
我在这里使用它:
<FormProvider {...methods}>
<form onSubmit={handleSubmit((data) => console.log(data))}>
<FormInput name="firstName" label="First Name" />
<FormInput name="lastName" label="Last Name" />
<FormInput name="address1" label="Address 1" />
<FormInput name="email" label="Email" />
<FormInput name="zip" label="ZIP code" />
<Button
type="submit" …Run Code Online (Sandbox Code Playgroud) 我在一个子组件中使用 useFormContext 。这是它的代码:
const { register } = useFormContext<CustomerProfileFormData>();
Run Code Online (Sandbox Code Playgroud)
我如何模拟 useFormContext 以便我可以测试子组件。这是测试
it('should render properly', () => {
render(<AddressDetails isEdit={false} />);
expect(screen.getByTestId('address-details')).toBeInTheDocument();
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误 TypeError: Cannot destruct property 'register' of '(0 , _reactHookForm.useFormContext)(...)' 因为它是 null.Jest。
这是有道理的,因为我没有模拟 useFormContext 。我怎样才能模拟它?任何帮助将不胜感激。
react-hook-form ×10
reactjs ×10
material-ui ×3
javascript ×2
react-hooks ×2
typescript ×2
persistence ×1
ref ×1