Abe*_*ojo 6 javascript forms typescript reactjs formik
初始道具更改后,需要更新表单中的值
export default withFormik({
mapPropsToValues: (props: Props) => {
return (
{
id: props.initialData.id,
name: props.initialData.name
}
);
},
handleSubmit: (values, { props: Props, setSubmitting }) => {
Props.submitHandler(values);
},
})(NewDatasourceForm);
Run Code Online (Sandbox Code Playgroud)
在这里,mapPropsToValues我能够得到新的props,但形式的值不会得到更新.
```const NewDatasourceForm =(props)=> {const {values,touching,errors,isSubmitting,setFieldValue,handleSubmit,handleChange,handleBlur} = props;
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>
);
};
Run Code Online (Sandbox Code Playgroud)
https://codesandbox.io/s/k5w5qn94z7
感谢你的支持.
Abe*_*ojo 22
withFormik({
enableReinitialize: true,
mapPropsToValues: (props: Props) => {
Run Code Online (Sandbox Code Playgroud)
添加enableReinitialize: true,解决了这个问题
https://github.com/jaredpalmer/formik/issues/168
| 归档时间: |
|
| 查看次数: |
12189 次 |
| 最近记录: |