29
loading...
This website collects cookies to deliver better user experience
npm install formik yup
import React from "react";
const App = () => {
return (
<form >
<input
placeholder="Type your First Name"
/>
<small>First Name Error</small>
<input
placeholder="Type your Last Name"
/>
<small>Last Name Error</small>
<button type="submit">
Submit
</button>
</form>
);
};
export default App;
import React from "react";
import { useFormik } from "formik";
import * as yup from "yup";
// ...
import React from "react";
import { useFormik } from "formik";
import * as yup from "yup";
const schema = yup.object().shape({
firstName: yup.string().min(3).required(),
lastName: yup.string().min(3).required(),
});
// ...
const App = () => {
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
},
validationSchema: schema,
onSubmit: handleOnSubmit,
});
// ...
};
const App = () => {
const handleOnSubmit = (values) => {
const fullName = Object.keys(values)
.map((key) => values[key])
.join(" ");
alert(`Hello ${fullName}!`);
};
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
},
validationSchema: schema,
onSubmit: handleOnSubmit,
});
// ...
};
import React, { useCallback } from "react";
// ...
const App = () => {
// ...
const setInputValue = useCallback(
(key, value) =>
formik.setValues({
...formik.values,
[key]: value,
}),
[formik]
);
return (
// ...
);
};
const App = () => {
// ...
return (
<form onSubmit={formik.handleSubmit}>
<input
placeholder="Type your First Name"
value={formik.values.firstName}
onChange={(e) => setInputValue("firstName", e.target.value)}
/>
<small>{formik.errors.firstName}</small>
<input
placeholder="Type your Last Name"
value={formik.values.lastName}
onChange={(e) => setInputValue("lastName", e.target.value)}
/>
<small>{formik.errors.lastName}</small>
{!!formik.errors.lastName && <br />}
<button type="submit" disabled={!formik.isValid}>
Submit
</button>
</form>
);
};
import React, { useCallback } from "react";
import { useFormik } from "formik";
import * as yup from "yup";
const schema = yup.object().shape({
firstName: yup.string().min(3).required(),
lastName: yup.string().min(3).required(),
});
const App = () => {
const handleOnSubmit = (values) => {
const fullName = Object.keys(values)
.map((key) => values[key])
.join(" ");
alert(`Hello ${fullName}!`);
};
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
},
validationSchema: schema,
onSubmit: handleOnSubmit,
});
const setInputValue = useCallback(
(key, value) =>
formik.setValues({
...formik.values,
[key]: value,
}),
[formik]
);
return (
<form onSubmit={formik.handleSubmit}>
<input
placeholder="Type your First Name"
value={formik.values.firstName}
onChange={(e) => setInputValue("firstName", e.target.value)}
/>
<small>{formik.errors.firstName}</small>
<input
placeholder="Type your Last Name"
value={formik.values.lastName}
onChange={(e) => setInputValue("lastName", e.target.value)}
/>
<small>{formik.errors.lastName}</small>
{!!formik.errors.lastName && <br />}
<button type="submit" disabled={!formik.isValid}>
Submit
</button>
</form>
);
};
export default App;