From 5c8008554c7a7a13b2fe973a1bd1694fada5ccfd Mon Sep 17 00:00:00 2001 From: Shubh Porwal Date: Sun, 28 Jul 2024 17:29:53 +0530 Subject: [PATCH] feat: `react-hook-form` example --- with-react-hook-form/App.js | 114 +++++++++++++++++++++++++++ with-react-hook-form/README.md | 51 ++++++++++++ with-react-hook-form/babel.config.js | 6 ++ with-react-hook-form/package.json | 20 +++++ 4 files changed, 191 insertions(+) create mode 100644 with-react-hook-form/App.js create mode 100644 with-react-hook-form/README.md create mode 100644 with-react-hook-form/babel.config.js create mode 100644 with-react-hook-form/package.json diff --git a/with-react-hook-form/App.js b/with-react-hook-form/App.js new file mode 100644 index 00000000..baaebefd --- /dev/null +++ b/with-react-hook-form/App.js @@ -0,0 +1,114 @@ +import { View, Text, StyleSheet, TextInput, Button } from "react-native"; +import { useForm, Controller } from "react-hook-form"; + +const EMAIL_REGEX = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/; + +export default function App() { + const { + control, + handleSubmit, + formState: { errors }, + } = useForm({ + defaultValues: { + email: "", + password: "", + }, + }); + + const onSubmit = (values) => { + const { email, password } = values; + + alert(`Email: ${email}, Password: ${password}`); + }; + + return ( + + Email + ( + + )} + rules={{ + required: "Email is required.", + pattern: { + value: EMAIL_REGEX, + message: "Enter a valid email address.", + }, + }} + /> + {errors.email && {errors.email.message}} + + Password + ( + + )} + rules={{ + required: "Password is required.", + minLength: { + value: 6, + message: "Password must be at least 6 characters long.", + }, + }} + /> + {errors.password && ( + {errors.password.message} + )} + + +