0

So I have this component:

const TextInput = ({ type = 'text', fullWidth = false, ...rest }) => {
  return (
    <input
      type={type}
      className={`text-input ${classNames([fullWidth, 'fullwidth'])}`}
      {...rest}
    />
  );
};

and then I have this code:

const { register } = useForm();

//then in the return
<TextInput type='email' fullWidth {...register('email')} />

And I'm getting this error:

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

How can I solved this? From what I looked I have to pass refs to the input but I don't know how to do this..

0

2 Answers 2

2
const TextInput = ({ register ,name,type = 'text', fullWidth = false, ...rest }) => {
  return (
    <input
      type={type}
      {...register(name)}
      className={`text-input ${classNames([fullWidth, 'fullwidth'])}`}
      {...rest}
    />
  );
};

And then you can do

const { register } = useForm();

//then in the return
<TextInput type='email' fullWidth register={register} />
Sign up to request clarification or add additional context in comments.

Comments

1

Okay so what I needed to do is use forwardRef like this:

const TextInput = React.forwardRef(({ type = 'text', fullWidth = false, ...rest }, ref) => {
  return (
    <input
      type={type}
      className={`text-input ${classNames([fullWidth, 'fullwidth'])}`}
      {...rest}
    />
  );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.