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

Created on 3 Mar 2020  路  3Comments  路  Source: unform/unform

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

Quando uso a valida莽茫o no form de acordo com a documenta莽茫o.

export default function AddProduct() {
  const formRef = useRef(null);

  async function handleSubmit(data) {
    try {
      formRef.current.setErrors({});

      const schema = Yup.object().shape({
        name: Yup.string().required('O nome do 茅 obrigat贸rio'),
        description: Yup.string().required('A descri莽茫o 茅 obrigat贸ria'),
        price: Yup.number()
          .typeError('Pre莽o obrigat贸rio')
          .required('O pre莽o 茅 obrigat贸rio')
          .positive(),
        code: Yup.string().required('O c贸digo 茅 obrigat贸rio'),
        img_id: Yup.mixed().required('A imagem 茅 obrigat贸ria'),
      });

      await schema.validate(data, {
        abortEarly: false,
      });

      console.tron.log(data);

      if (data.file === undefined) {
        document.getElementById('error-file').innerHTML =
          'A imagem 茅 obrigat贸ria';
      }

      console.tron.log(data);
    } catch (err) {
      const validationErrors = {};

      if (err instanceof Yup.ValidationError) {
        console.tron.log(err);
        err.inner.forEach(error => {
          validationErrors[error.path] = error.message;
        });

        formRef.current.setErrors(validationErrors);
      }
    }
  }
  return (
    <ContainerProduct>
      <h1>Adicione Novo Produto</h1>
      <Form ref={formRef} onSubmit={handleSubmit}>
        <div>
          <ImgInput name="img_id" />
          <span id="error-file" />
        </div>
        <div>
          <Input name="name" placeholder="Nome" />
          <Textarea rows="10" name="description" placeholder="Descri莽茫o" />
          <Input type="number" name="price" placeholder="Pre莽o" />
          <Input name="code" placeholder="C贸digo" />
          <button type="submit">Enviar</button>
        </div>
      </Form>
    </ContainerProduct>
  );
}
bug

Most helpful comment

I was with the same warning here.

Fixed exporting my input component with forwardRef (as the warning message suggest)

import React, { forwardRef } from 'react';
...
export default forwardRef(Input);

All 3 comments

Hello @souza-gsdev, i cannot reproduce the problem here.

Can you provide a Code Sandbox with the same code with the error?

Also, keep in mind to use english as primary language inside this repository :)

I was with the same warning here.

Fixed exporting my input component with forwardRef (as the warning message suggest)

import React, { forwardRef } from 'react';
...
export default forwardRef(Input);

Closing this since there are no updates from the author.

Feel free to reopen the issue if the problem persists.

Was this page helpful?
0 / 5 - 0 ratings