Created
March 8, 2019 01:16
-
-
Save khoberg/e404f20432ec39c67aa580b589ccad4f to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import {Formik} from "formik"; | |
class FormikState { | |
public input1: string; | |
public input2: string; | |
public nested: FormikNestedState; | |
constructor() { | |
this.input1 = ""; | |
this.input2 = ""; | |
this.nested = new FormikNestedState(); | |
} | |
isInput1Empty() { | |
return this.input1 === ""; | |
} | |
} | |
class FormikNestedState { | |
public input3: string; | |
constructor() { | |
this.input3 = ""; | |
} | |
isInput3Empty() { | |
return this.input3 === ""; | |
} | |
} | |
export default class FormikStateClassExample extends React.Component { | |
render() { | |
return ( | |
<Formik | |
initialValues={new FormikState()} | |
onSubmit={console.log}> | |
{(formik) => { | |
console.log(formik.values); | |
return ( | |
<div> | |
<input name="input1" onChange={formik.handleChange} onBlur={formik.handleBlur}/> | |
<input name="input2" onChange={formik.handleChange} onBlur={formik.handleBlur}/> | |
<input name="nested.input3" onChange={formik.handleChange} onBlur={formik.handleBlur}/> | |
<pre> | |
{JSON.stringify(formik.values)} | |
{formik.values.isInput1Empty()} | |
{formik.values.nested.isInput3Empty()} | |
</pre> | |
</div> | |
) | |
}} | |
</Formik> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment