Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  170] [ 6]  / answers: 1 / hits: 20722  / 6 Years ago, mon, march 26, 2018, 12:00:00

Once the initial props changes, the values in the forms need to be updated



export default withFormik({
mapPropsToValues: (props: Props) => {
return (
{
id: props.initialData.id,
name: props.initialData.name
}
);
},
handleSubmit: (values, { props: Props, setSubmitting }) => {
Props.submitHandler(values);
},
})(NewDatasourceForm);


Here in mapPropsToValues I am able to get the new props, But the values in form not get updated.



const NewDatasourceForm = (props) => {
const {
values,
touched,
errors,
isSubmitting,
setFieldValue,
handleSubmit,
handleChange,
handleBlur
} = props;

const _handleSelect = (selectDSChoice) => {
try {
setFieldValue('dataSourceType', selectDSChoice.value);
} catch (error) {
// tslint:disable-next-line:no-console
console.error(error);
}

};

return(
<form className=needs-validation was-validated p-5 onSubmit={handleSubmit}>

<div className=form-group>
<label>Name</label>
<input
className={`form-control`}
name=name
type=text
value={values.name}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
</form>
);
};


https://codesandbox.io/s/k5w5qn94z7



Thanks for support.


More From » forms

 Answers
10
withFormik({
enableReinitialize: true,
mapPropsToValues: (props: Props) => {


Add enableReinitialize: true, solved the issue



https://github.com/jaredpalmer/formik/issues/168


[#54854] Thursday, March 22, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hakeemabramh

Total Points: 234
Total Questions: 109
Total Answers: 109

Location: Romania
Member since Mon, Jun 6, 2022
2 Years ago
hakeemabramh questions
;