【发布时间】:2019-05-24 00:18:01
【问题描述】:
我在我的反应应用程序中使用 formik 库。最初,我在构造函数中声明了状态值,在 componentDidMount 中我正在调用一个 API,该应用程序响应正在更新状态值,任何人都可以帮助我将状态值传递给 fomik 初始值
在我的情况下,formik 采用在构造函数中声明的初始状态值。提前感谢
class Dropdown extends Component {
constructor(props) {
super(props);
this.state = {
//some data
};
}
componentDidMount() {
//api call
this.setState( {
//update state values with api response data
});
}
render() {
return (
<Formik
initialValues={this.state}
validate={validate(validationSchema)}
onSubmit={onSubmit}
render={
({
values,
errors,
touched,
status,
dirty,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
isValid,
handleReset,
setTouched
}) => (
//form uses initialValues
)} />
)
}
}
【问题讨论】: