pass a submit function to formik onSubmit prop.
formik
onSubmit
// submit function submitValues = ({ name, email, confirmPassword, password }) => { console.log({ name, email, confirmPassword, password }); };
<Formik render={props => <Form {...props} />} initialValues={values} validationSchema={validationSchema} onSubmit={this.submitValues} />
Formik then provides you with a handleSubmit prop which you use to handle submit in your form.
handleSubmit
<form onSubmit={handleSubmit}> ...... </form>
handleSubmit basically passes all the values in your form to the submit function you passed to onSubmit
I didn't use state for my initial values in the article but it's possible use state.
create state values
this.state={ username:'' }
then in your componentdidmount or where ever you make your database calls you can setstate of that value.
componentdidmount
componentdidmount(){ //database call this.setState({username: databaseUsername}) }
then pass it to the Formik's initialValues prop
Formik
initialValues
render(){ const {username} = this.state const values = {username: username} return( <Formik render={props => <Form {...props} />} initialValues={values} validationSchema={validationSchema} onSubmit={this.submitValues} /> ) }
Link to demo
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
How can I get the values from the form to submit them?
pass a submit function to
formik
onSubmit
prop.Formik then provides you with a
handleSubmit
prop which you use to handle submit in your form.handleSubmit
basically passes all the values in your form to the submit function you passed toonSubmit
how can I set the value to the state if it has been fetched from the database
I didn't use state for my initial values in the article but it's possible use state.
create state values
then in your
componentdidmount
or where ever you make your database calls you can setstate of that value.then pass it to the
Formik
'sinitialValues
propLink to demo