Skip to content

Instantly share code, notes, and snippets.

@sibelius
Last active April 13, 2023 20:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/0c4bb9f1ef7e166587a97695445e0729 to your computer and use it in GitHub Desktop.
Save sibelius/0c4bb9f1ef7e166587a97695445e0729 to your computer and use it in GitHub Desktop.
Example showing how to useFormik and FormikProvider
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;
@ericktatsui
Copy link

Nice example, I just would remove this double space at line 7, right after submit:.
enqueueSnackbar(submit: ${JSON.stringify(values)}, {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment