Last active
March 10, 2025 03:04
-
-
Save sibelius/0c4bb9f1ef7e166587a97695445e0729 to your computer and use it in GitHub Desktop.
Example showing how to useFormik and FormikProvider
This file contains 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
type Props = { | |
}; | |
const FormUseFormik = (props: Props) => { | |
const { enqueueSnackbar } = useSnackbar(); | |
const onSubmit = (values) => { | |
enqueueSnackbar(`submit: ${JSON.stringify(values)}`, { | |
preventDuplicate: true, | |
persist: false, | |
}); | |
}; | |
const formikbag = useFormik<Values>({ | |
initialValues: { | |
email: '', | |
password: '', | |
}, | |
onSubmit, | |
}); | |
const { values, setFieldValue, handleSubmit } = formikbag; | |
return ( | |
<Flex flex={1}> | |
<Card width={800}> | |
<Text mb={20}>Simple Formik</Text> | |
<FormikProvider value={formikbag}> | |
<Flex flexDirection='column'> | |
<TextField | |
placeholder='email' | |
mb={20} | |
value={values.email} | |
onChange={(e) => setFieldValue('email', e.target.value)} | |
/> | |
<TextField | |
type='password' | |
placeholder='password' | |
mb={20} | |
value={values.password} | |
onChange={(e) => setFieldValue('password', e.target.value)} | |
/> | |
</Flex> | |
<Button | |
type='submit' | |
onClick={handleSubmit} | |
> | |
Submit | |
</Button> | |
</FormikProvider> | |
</Card> | |
</Flex> | |
) | |
}; | |
export default FormUseFormik; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice example, I just would remove this double space at line 7, right after
submit:
.enqueueSnackbar(
submit: ${JSON.stringify(values)}, {