[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]