Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form reset, append adds ghost empty fields #11836

Open
1 task done
imprakharshukla opened this issue May 1, 2024 · 5 comments
Open
1 task done

Form reset, append adds ghost empty fields #11836

imprakharshukla opened this issue May 1, 2024 · 5 comments
Labels
status: need more detail Please follow our issue template.

Comments

@imprakharshukla
Copy link

imprakharshukla commented May 1, 2024

Version Number

7.51.3

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/react-hook-forms-appenderror-97c7km?file=%2Fsrc%2FApp.tsx%3A813%2C28

Steps to reproduce

  1. Click on "Add Inventory" button to append a field after the initial data has been set.
  2. Click on "Update Product" and observe the console log.
    The first click logs an empty error object and then the next click suddenly adds about 12 fields in the inventory key.

Expected behaviour

The extra 12 ghost empty inventory fields should not be added.

What browsers are you seeing the problem on?

Firefox, Chrome, Safari, Edge

Relevant log output

image

[
    null,
    {
        "variant": {
            "message": "Array must contain at least 1 element(s)",
            "type": "too_small"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    },
    {
        "size": {
            "message": "Required",
            "type": "invalid_type"
        },
        "variant": {
            "message": "Required",
            "type": "invalid_type"
        }
    }
]

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Rudomitori
Copy link

I faced a bug that looks similar to the described here.

I've tried different versions of react-hook-form and found that the bug was introduced in version 7.38.0

@imprakharshukla
Copy link
Author

imprakharshukla commented May 3, 2024

@Rudomitori Just tested versions till 7.32.0 and the ghost fields are still created. Makes me wonder if there's anything wrong with the way I am initialising the form (though I have tired to do it in multiple ways and it still happens).

@bluebill1049
Copy link
Member

clean up your codesandbox.

@bluebill1049 bluebill1049 added the status: need more detail Please follow our issue template. label May 5, 2024
@Rudomitori
Copy link

It took some time but I finally managed to reproduce my use case.
Codesandbox

Steps:

  1. Click "Append"
  2. Enter something into the array fields
  3. Click "Reset"
    The array fields disappears
  4. Click "Append"
  5. Enter something into the array fields
  6. Click "Reset"
    Expected: Result is the same as after the step 3
    Actual: The array fields are still there, the first field isn't changed, the second field becomes empty

@erashu212
Copy link

erashu212 commented May 9, 2024

@imprakharshukla You can fix this issue by passing
This is happening because of this line

 const onReset = () => {
    reset({}, { keepDefaultValues: true });
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: need more detail Please follow our issue template.
Projects
None yet
Development

No branches or pull requests

4 participants