Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185500
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T00:02:53+00:00 2022-06-10T00:02:53+00:00

reactjs – React hooks rules-of-hooks error in classes with Yup

  • 0

[ad_1]

So before updating my app to [email protected], I didn’t have this error. I’ve followed this guide to make the upgrade. This is what I’m doing.

I’ve created a custom hook to implement Yup and set customized messages, use context, and other custom hooks:

    import * as Yup from 'yup';
    import { useAppContext } from '../context/AppContext';
    import { Locale } from '../utils/locale';
    import useLabel from './useLabel';

    const useYup = () => {
      const { state } = useAppContext();
      const { locale } = state;
      const { getLabel } = useLabel();

      const { requiredMessage } = {
        [Locale.PT_BR]: {
          requiredMessage: 'Este campo é obrigatório',
          // other messages
        },
        [Locale.EN_US]: {
          requiredMessage: 'This field is required',
          //other messages
        },
      }[locale];

      const name = Yup.string().min(2, minMessage(2)).max(150, maxMessage(150)).required(requiredMessage);
      // other fields 

      return { name };
    };

    export default useYup;

I’m using this in a lot of model classes for field validation, like this:


    import * as Yup from 'yup';
    import useYup from '../hooks/useYup';
    import { BusinessExpense } from './BusinessExpense.entity';

    export class Business {

      // other attributes
      businessName?: string = '';


      public static get validationSchema() {
        const { name: businessName } = useYup();

        return Yup.object({
          businessName
        });
      }
    }

From this, I’m using the validation schema with formik as recommended by the library, for example:

     const { values, handleChange, errors, touched, handleSubmit } = useFormik({
        initialValues: new Business(),
        validationSchema: Business.validationSchema,
        onSubmit,
      });

Finally, after the upgrade, it started to throw the error below. Any suggestions on how to fix it?
I really appreciate any help you can provide.

src\model\Business.entity.ts
  Line 16:28:  React Hook "useYup" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function  react-hooks/rules-of-hooks
Failed to compile.

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 318 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 290 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 285 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.