小编R-d*_*ijn的帖子

ForwardRef 警告 React-hook-forms with Material UI TextField

我正在尝试使用带有 Material UI 输入的 react-hook-forms 构建一个表单(在这种情况下是我的 TextField 自定义变体)。尽管表单看起来完全正常,但它在呈现表单时会在控制台中触发一条警告消息。

警告:不能为函数组件提供引用。尝试访问此引用将失败。你的意思是使用 React.forwardRef() 吗?

我正在使用 react-hook-form 的 Controller 来包装我的 TextField(如文档所建议的那样)

非常欢迎任何建议或解决方案!

在出现此问题的 TextField 组件和表单下方:

组件文本字段

const TextField = props => {
    const {
        icon,
        disabled,
        errors,
        helperText,
        id,
        label,
        value,
        name,
        required,
        ...rest
      } = props;

    const classes = useFieldStyles();
    
    return (
        <MuiTextField 
            {...rest}
            name={name}
            label={label}
            value={value || ''}
            required={required}
            disabled={disabled}
            helperText={helperText}
            error={errors}
            variant="outlined" 
            margin="normal" 
            color="primary"
            InputProps={{
                startAdornment: icon,
                classes: {
                    notchedOutline: classes.outline,
                },
            }}
            InputLabelProps={{
                className: classes.inputLabel,
            }}
        />
    )
};

TextField.propTypes …
Run Code Online (Sandbox Code Playgroud)

reactjs material-ui react-hook-form

4
推荐指数
1
解决办法
4282
查看次数

标签 统计

material-ui ×1

react-hook-form ×1

reactjs ×1