Issue
I'm developing an app using Capacitor, Ionic, React and recently I faced the following error, while using React Hook Form and YupResolver for the first time:
When I try to run the project, I get this error:
Failed to compile
./node_modules/@hookform/resolvers/dist/resolvers.module.js
Attempted import error: 'set' is not exported from 'react-hook-form' (imported as 'o').
I want to create and validate a form for changing Password, submitting the new Password to an external API /change-password. The form will be like below:
Actual Password: ...
New Password: ...
Confirm new Password: ...
Submit
The component:
import {
IonContent,
IonPage,
IonItem,
IonLabel,
IonButton,
IonInput,
IonRow,
IonAlert,
IonGrid,
IonCol,
} from "@ionic/react";
import React, { useState } from "react";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import axios from "axios";
// form validation rules
const validationSchema = yup.object().shape({
password: yup
.string()
.min(8, "Password must be at least 8 characters")
.required("Password is required"),
newPassword: yup
.string()
.min(8, "Password must be at least 8 characters")
.required("New Password is required"),
confirmPassword: yup
.string()
.oneOf([yup.ref("newPassword"), null], "Passwords must match")
.required("Confirm Password is required"),
});
const ChangePassword: React.FC = () => {
//get the actual password from React Contenxt
const {
password,
setPassword,
alertMessage,
setAlertMessage,
} = React.useContext(AuthContext);
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
// functions to build form returned by useForm() hook
const { register, handleSubmit, errors } = useForm({
resolver: yupResolver(validationSchema),
});
const onSubmit = () => {
const data = {
oldPassword: password,
newPassword: newPassword,
sourceId: 1,
};
axios
.post("change-password", data)
.then((response) => {
return response.data;
});
};
return (
<React.Fragment>
<IonPage className="ion-page" id="main-content">
<IonContent className="ion-padding">
<IonGrid>
<h3>Change Password</h3>
<form onSubmit={handleSubmit(onSubmit)}>
<IonItem>
<IonLabel position="floating">Actual Password</IonLabel>
<IonInput
name="password"
type="password"
value={password}
ref={register}
className={`form-control ${
errors.password ? "is-invalid" : ""
}`}
onIonChange={(e) => setPassword(e.detail.value!)}
></IonInput>
<div className="invalid-feedback">
{errors.password?.message}
</div>
</IonItem>
<IonItem>
<IonLabel position="floating">New Password</IonLabel>
<IonInput
name="newPassword"
type="password"
value={newPassword}
ref={register}
className={`form-control ${
errors.newPassword ? "is-invalid" : ""
}`}
onIonChange={(e) => setNewPassword(e.detail.value!)}
></IonInput>
<div className="invalid-feedback">
{errors.newPassword?.message}
</div>
</IonItem>
<IonItem>
<IonLabel position="floating">
Cofirm New Password
</IonLabel>
<IonInput
name="confirmPassword"
type="password"
value={confirmPassword}
ref={register}
className={`form-control ${
errors.confirmPassword ? "is-invalid" : ""
}`}
onIonChange={(e) => setConfirmPassword(e.detail.value!)}
></IonInput>
<div className="invalid-feedback">
{errors.confirmPassword?.message}
</div>
</IonItem>
<IonButton type="submit">
Submit
</IonButton>
</form>
</IonGrid>
</IonContent>
</IonPage>
</React.Fragment>
);
};
export default ChangePassword;
@hookform/[email protected]
[email protected]
[email protected]
Any help would be really appreciated. Thanks!
Solution
I didn't find a solution specifically for this error. So I decided to move on with the implementation and upgraded RHF from 6.x.x to the last version 7.1.1, since I'm using the latest version of Resolvers too. https://react-hook-form.com/migrate-v6-to-v7/ I also adjusted the implementation based on RHF version 7 documentation: https://react-hook-form.com/api/useform
Now it is working properly.
Answered By - Ani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.