【发布时间】:2019-02-11 20:45:02
【问题描述】:
我正在使用我的表单和 react redux。我有相同的表格来编辑和创建一个新表格。
我希望,在打开我的表单时,如果进行编辑,我会从我的 api 中输入表单的初始值。
我已经尝试过官方文档,但它不起作用。
这是我的表格:
class Form extends Component {
componentDidMount () {
const tripId = this.props.match.params.uid;
if(tripId){
this.fetchData(tripId);
}
}
fetchData = async (tripId) => {
axios.defaults.headers.common['Authorization'] = 'token';
axios.defaults.headers.common['Accept'] = 'application/vnd.trips.v1+json';
await axios.get(`myurl`)
.then(res => {
const trip = res.data;
this.setState({
trip: trip,
isLoading: false,
initialValues: trip
});
})
console.log('Terminou de carregar')
}
(....)
Form = reduxForm({ form: 'trip', enableReinitialize : true })(Form)
const mapStateToProps = state => {
const { intl, vehicleTypes, users, dialogs, trip } = state
return {
intl,
vehicleTypes,
users,
dialogs,
initialValues: trip
}
}
export default connect(
mapStateToProps, { setDialogIsOpen }
)(injectIntl(withRouter(withTheme()(Form))))
但我的 initialValues 从未填充,总是空白。我调试了代码,我看到我的 API 正在加载并设置我的 fetchData 方法的状态。那么,我在这里做错了什么?
【问题讨论】:
标签: reactjs react-redux redux-form