There are a couple of things you need to check:
The
schema
object does not depend on any local-scoped variables. You can put it outside of the function so the component doesn’t recreate it every time it rendersPass the yup
schema
object to ayupResolver
first instead of passing directly to theresolver
import React from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from "yup";
const schema = yup.object().shape({
patientIdentity: yup.object().shape({
firstName: yup.string().required('Required field'),
lastName: yup.string().required('Required field'),
}),
});
const App = () => {
const { ... } = useForm({
resolver: yupResolver(schema),
});
return (...);
};
- If you write validation rules using third-party library like
Yup
, you can remove therules
props from theController
since they’re duplicated.
CLICK HERE to find out more related problems solutions.