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

Infinity loop if the value is NaN #11

Open
eugenekgn opened this issue Jun 27, 2019 · 1 comment
Open

Infinity loop if the value is NaN #11

eugenekgn opened this issue Jun 27, 2019 · 1 comment

Comments

@eugenekgn
Copy link

It seems like there's an infinite loop that <onChange name={value}/> throws if the watched value happens to be NaN

@leighmetzroth
Copy link

I've also run into this and narrowed it down to the fact that NaN !== NaN. If you use the lodash function isEqual for the equality comparison then it should work a lot more reliably.

e.g.

    if (!isEqual(value, previous)) {
      this.setState({ previous: value })
      children(value, previous)
    }

Below is a failing test that confirms the issue (it fails due to "Maximum update depth exceeded.").

  it('should only invoke the listener once when a number field is cleared', async () => {
    const spy = jest.fn();
    render(
      <Form onSubmit={() => {}} initialValues={{ value: 1 }}>
        {() => (
          <form>
            <Field<number> name="value">
              {({ input: { onChange, value, ...inputRest } }) => (
                <input
                  {...inputRest}
                  value={value}
                  type="number"
                  onChange={e => onChange(e.target.valueAsNumber)}
                />
              )}
            </Field>
            <OnChange name="value">{spy}</OnChange>
          </form>
        )}
      </Form>
    );

    expect(spy).not.toHaveBeenCalled();

    await userEvent.clear(getByRole('spinbutton'));

    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(NaN, 1);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants