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

disabled state causes infinity loop and freezes entire app #11287

Closed
1 task done
jantimon opened this issue Dec 12, 2023 · 0 comments
Closed
1 task done

disabled state causes infinity loop and freezes entire app #11287

jantimon opened this issue Dec 12, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@jantimon
Copy link
Contributor

jantimon commented Dec 12, 2023

Version Number

7.49.0

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/disabled-react-hook-forms-ntj84p

import { useForm, SubmitHandler } from "react-hook-form";

type Inputs = {
  exampleRequired: string;
};

export default function App() {
  const { register, handleSubmit, formState, getFieldState } =
    useForm<Inputs>();
  const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);

  console.log(getFieldState("example", formState));

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input
        {...register("exampleRequired", { required: true, disabled: true })}
      />
      {formState.errors.exampleRequired && <span>This field is required</span>}
      <input type="submit" />
    </form>
  );
}

Steps to reproduce

PR with a failing test: #11288

shot-bb5FUpcW

Manual:

  1. Open Sandbox
  2. Click submit for a disabled field
  3. Form hands in endless loop:
    shot-gLJ0FSsX

Expected behaviour

disabled in combination with getFieldState should not cause a never ending infinity loop

What browsers are you seeing the problem on?

Firefox, Chrome, Safari, Edge

Relevant log output

we saw that the loop is _updateDisabledField -> updateTouchAndDirty

updateTouchAndDirty:

_formStateDirtyFields === {}
_proxyFormState.dirtyFields === "all"
isPreviousDirty: undefined
isBlurEvent: false
shouldUpdateField: true

shouldRenderFormState returns "dirtyFields"

Screenshot

loop

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049 bluebill1049 added the bug Something isn't working label Dec 12, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 11, 2024
rafaelcalhau pushed a commit to rafaelcalhau/react-hook-form that referenced this issue May 5, 2024
…ate (react-hook-form#11291)

* 🐞 fix react-hook-form#11287 disabled prop cause const dirtyFields update

* fix issue with test
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants