Skip to content

issue: Infinite re-render when init with errors #12599

@gbataille

Description

@gbataille

Version Number

7.53.0

Codesandbox/Expo snack

https://codesandbox.io/p/devbox/interesting-spence-kgg47d

Steps to reproduce

Careful the link in codesandbox can crash your tab.
You have to open the dev console to see the infinite re-render

I think what I'm doing is "wrong" in the sense that it does not make sense (and I have now fixed my usage) but I don't understand what's happening and I think anyway there should be some protection against it.

It seems that

  • if you initialize your useForm with errors: {}
  • monitor the errors state with {formState: {errors}} = useFormContext()
    You get an infinite re-render
export const FormAmountShowcase = () => {
    const formProps = useForm<{ foo: string }>({
        defaultValues: {foo: 'bar'},
        errors: {},
    })

    return (
        <FormProvider {...formProps}>
            <FormComp/>
        </FormProvider>
    )
}

const FormComp = () => {
    console.log('rendering')
    const {formState: {errors}} = useFormContext<{ foo: string }>()

    return <>Foo</>
}

Expected behaviour

no infinite re-render.
Either the input is accepted, or it should fail when this value is used.

What browsers are you seeing the problem on?

Chrome

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    not validnot valid issue, question or suggestionwont be worked onThis will not be worked on

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions