简体   繁体   English

类型错误:无法定义属性“当前”:Object 不可扩展

[英]TypeError: can't define property “current”: Object is not extensible

I have a react component InputField :我有一个反应组件InputField

export default function InputField({ name, type, placeholder, className }, ref) {
  return (
    <input
      name={name}
      type={type}
      placeholder={placeholder}
      className={InputTailwindStyle}
      ref={ref}
    />
  );
}

and I try to put it inside another component LoginForm where I am using react-hook-form to handle my form hooks:我尝试将它放在另一个组件LoginForm中,在该组件中我使用react-hook-form来处理我的表单挂钩:

import { useForm } from 'react-hook-form';

(some code ... )

const { register, handleSubmit } = useForm();

<InputField
        name='password'
        type='password'
        placeholder='Password'
        ref={register}
/>

but I get this error: TypeError: cannot define property "current": Object is not extensible但我收到此错误: TypeError: cannot define property "current": Object is not extensible

You need to use React.forwardRef for property ref to be valid in function component:您需要使用React.forwardRef使属性ref在 function 组件中有效:

function InputField({ name, type, placeholder, className }, ref) {
  return (
    <input
      name={name}
      type={type}
      placeholder={placeholder}
      className={`py-2 text-cTxt placeholder-gray-400 bg-transparent border-b-2 my-5 border-cBtn rounded px-2 ${className}`}
      ref={ref}
    />
  );
}

export default React.forwardRef(InputField);

暂无
暂无

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

相关问题 未捕获的类型错误:无法定义属性“x”:对象不可扩展 - Uncaught TypeError: can't define property "x": Object is not extensible 未捕获的TypeError:无法添加属性12,对象不可扩展 - Uncaught TypeError: Can't add property 12, object is not extensible React Error(TypeError):无法添加属性上下文,对象不可扩展 - React Error (TypeError): Can't add property context, object is not extensible 无法添加属性_tracking,对象不可扩展 - Can't add property _tracking, object is not extensible 代码在 codeSandbox 中运行良好,但在 IDE 中显示错误为“无法定义属性”电子邮件“:Object 不可扩展” - The code is working fine in codeSandbox, But showing error while doing in the IDE as “can't define property ”email“: Object is not extensible” 无法定义属性“ _getObservable”:对象不可扩展 - Cannot define property '_getObservable': object is not extensible TypeError:无法添加属性 0,object 不可扩展 - TypeError: Cannot add property 0, object is not extensible 无法在react中分配新属性,得到了“无法添加属性xxx,对象不可扩展” - Can not assign new property in react , got ` Can't add property xxx, object is not extensible` React App-TypeError:无法添加属性setState,对象不可扩展 - React App - TypeError: Cannot add property setState, object is not extensible 材料表类型错误:无法添加属性表数据,对象不可扩展 - Material-table TypeError: Cannot add property tableData, object is not extensible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM