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

Controller doesn't work with nextjs #1094

Closed
StKostyantin opened this issue Feb 24, 2020 · 13 comments
Closed

Controller doesn't work with nextjs #1094

StKostyantin opened this issue Feb 24, 2020 · 13 comments
Labels
bug Something isn't working

Comments

@StKostyantin
Copy link

I use antd and the same example works fine with create-react-app, but nextjs crashes.

For easier code reuse, I wrote my module for the form to connect antd and react-hook-form

Sample code for using normal inputs in my module:


import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { Form, Input } from 'antd';
import { useFormContext, Controller } from 'react-hook-form';

import { useNestedProperty } from './hooks';

const { Item: FormItem } = Form;

const RenderField = ({
  input,
  label,
  type,
  name,
  placeholder,
  required,
  hasFeedback,
  addonBefore,
  addonAfter
}) => {
  const { control } = useFormContext();
  const [validateStatus, setValidateStatus] = useState('');
  const { error } = useNestedProperty(name);

  useEffect(() => {
    setValidateStatus(error ? 'error' : 'success');
  }, [error]);

  return (
    <Controller
      name={name}
      control={control}
      as={
        <FormItem
          label={label}
          validateStatus={validateStatus}
          help={error}
          required={required}
          hasFeedback={hasFeedback}
        >
          <Input
            addonBefore={addonBefore}
            addonAfter={addonAfter}
            {...input}
            placeholder={label || placeholder}
            type={type}
          />
        </FormItem>
      }
    />

  );
};

RenderField.propTypes = {
  input: PropTypes.object,
  name: PropTypes.string.isRequired,
  type: PropTypes.string.isRequired,
  addonAfter: PropTypes.node,
  addonBefore: PropTypes.node,
  label: PropTypes.string,
  placeholder: PropTypes.string,
  meta: PropTypes.object,
  required: PropTypes.bool,
  hasFeedback: PropTypes.bool
};

export default RenderField;

This is how I use this code in creating a form:

import React from 'react';
import PropTypes from 'prop-types';
import { useForm, FormContext } from 'react-hook-form';
import { Form } from 'antd';
import { object, string } from 'yup';

import { RenderField } from '../../../forms';

const loginFormSchema = object().shape({
  email: string()
    .email('invalid email')
    .required('required field')
});

const LoginForm = () => {
  const methods = useForm({
    validationSchema: loginFormSchema
  });
  return (
    <div>
      <FormContext {...methods}>
        <Form onSubmit={methods.handleSubmit(data => console.log('data', data))} layout="vertical">
          <div>
            <RenderField name="email" type="text" placeholder="Your email" required />
            <RenderField name="password" type="password" placeholder="Your password" required />
          </div>
        </Form>
      </FormContext>
    </div>
  );
};

LoginForm.propTypes = {
  onLogin: PropTypes.func.isRequired,
  prevPathname: PropTypes.string
};
export default LoginForm;

And I see such an error:
image

Right now I'm using "next": "^ 9.2.2" and "react-hook-form": "^ 4.9.8"

@bluebill1049
Copy link
Member

  useEffect(() => {
    setValidateStatus(error ? 'error' : 'success');
  }, [error]);

try to comment out the above code and see if the errors goes away.

@bluebill1049 bluebill1049 added the question Further information is requested label Feb 24, 2020
@gordysc
Copy link

gordysc commented Feb 24, 2020

I was able to repro this issue as well, it looks like the issue was introduced somewhere between RHF v4.9.6 and v4.9.8. It does work (for me) w/ 4.9.6.

@bluebill1049 bluebill1049 added bug Something isn't working and removed question Further information is requested labels Feb 24, 2020
@bluebill1049
Copy link
Member

oh thanks @gordysc I will investigate on this issue. can you share a simple codesandbox with nextjs?

@bluebill1049
Copy link
Member

I shouldn't work with CRA as well, it's strange... I am sure there is something that I have broked.

@gordysc
Copy link

gordysc commented Feb 24, 2020

@bluebill1049 sure! I've created a basic repo to show the issue:
https://github.com/gordysc/rhf-example

Note: If you change ^4.9.6 to 4.9.6, blow away node_modules and yarn.lock to force 4.9.6, it'll work... but w/ 4.9.8, it causes the issue above. Thanks for the fast response!

@bluebill1049
Copy link
Member

thanks @gordysc i will take a look at during lunchtime or tonight.

@sdornan
Copy link

sdornan commented Feb 24, 2020

Looks like 4.9.7 is okay too, so the bug seems to have been introduced in 4.9.8.

@bluebill1049
Copy link
Member

Thanks @sdornan I think I know the root cause of this one, will try to send out a beta version for the fix when I am arriving at the office.

@bluebill1049
Copy link
Member

bluebill1049 commented Feb 24, 2020

can you guys plz help test this one: react-hook-form@4.9.9-beta.1?

bluebill1049 added a commit that referenced this issue Feb 24, 2020
@gordysc
Copy link

gordysc commented Feb 24, 2020

Works great for me! I imagine you tested this against my repo though, so would be interested to hear if it works for others. Thanks for the super fast turn around!

@bluebill1049
Copy link
Member

no worries @gordysc. one more confirm i will merge the PR, i will probably release this fix over the weekend, cause i have merge a new feature last weekend, need a bit more testing on it before let it out.

@StKostyantin
Copy link
Author

can you guys plz help test this one: react-hook-form@4.9.9-beta.1?

Thanks, works great for me!

@bluebill1049
Copy link
Member

Thanks @StKostyantin will release the minor version this weekend.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 15, 2022
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

4 participants