question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Controller doesn't work with nextjs

See original GitHub issue

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”

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
bluebill1049commented, Feb 24, 2020

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

3reactions
bluebill1049commented, Feb 24, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

how/where to put models/controllers in next js app?
This is how I look at it from a classic Model-View-Controller perspective: A Controller manipulates your model based on user interaction, ...
Read more >
WebXR controller models not loading in Next.js - Bugs
I got it almost working perfectly, but the WebXR controller models don't load and it defaults to the fallback controller meshes. In the...
Read more >
Advanced Features: Error Handling - Next.js
Error Handling. This documentation explains how you can handle development, server-side, and client-side errors. Handling Errors in Development.
Read more >
next-controller - npm
Next.js is an awesome framework, however it provides a primitive way to write API backend logics via a single function. It's simpler, but ......
Read more >
How to implement redirects in Next.js - LogRocket Blog
To solve this issue, you can use Next.js middleware. It enables you to execute code prior to a request being processed, allowing you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found