Last active
October 10, 2021 08:34
-
-
Save oukayuka/1ef7278c466fe926496ac2181a029f97 to your computer and use it in GitHub Desktop.
Formik sample with TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { InjectedFormikProps, withFormik } from 'formik'; | |
import * as React from 'react'; | |
import * as Yup from 'yup'; | |
interface FormValues { | |
login: string; | |
} | |
interface FormProps { | |
login?: string; | |
} | |
const InnerForm: React.SFC<InjectedFormikProps<FormProps, FormValues>> = ( | |
props, | |
) => ( | |
<form onSubmit={props.handleSubmit}> | |
<input | |
id="login" | |
placeholder="User name..." | |
type="text" | |
onChange={props.handleChange} | |
value={props.values.login} | |
/> | |
{props.touched.login && props.errors.login && | |
<div>{props.errors.login}</div>} | |
<button | |
type="submit" | |
disabled={props.isSubmitting} | |
> | |
Submit | |
</button> | |
</form> | |
); | |
const UserSearchForm = withFormik<FormProps, FormValues>({ | |
mapPropsToValues: () => ({ login: '' }), | |
validationSchema: Yup.object().shape({ | |
login: Yup.string() | |
.max(16, 'Please input 16 characters or less') | |
.required('Please input login name'), | |
}, | |
), | |
handleSubmit: (values, { setSubmitting }) => { | |
setTimeout( | |
() => { | |
alert(JSON.stringify(values, null, 2)); | |
setSubmitting(false); | |
}, | |
1000, | |
); | |
}, | |
})(InnerForm); | |
export default UserSearchForm; |
Thank's !
Thanks for simple form. Is there any way to get props value(eg. props.values.login) in componentWillUnmount()?
'InjectedFormikProps' is deprecated, use OuterProps & FormikProps<Values>
instead.
InjectedFormikProps seems deprecated!
this code is deprecated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks
InjectedFormikProps
was what i was looking for to get typescript to work