在ant3.0中 在组件直接可以动态更新
class UpdateForm extends Component {
static propTypes = {
categoryName: PropTypes.string.isRequired,
}
render() {
const {categoryName} = this.props
const { getFieldDecorator } = this.props.form
return (
<Form>
<Item>
{
getFieldDecorator('categoryName', {
initialValue: categoryName,
rules: [
{required: true, message: '分类名称必须输入'}
]
})(
<Input placeholder='请输入分类名称'/>
)
}
</Item>
</Form>
)
}
}
export default Form.create()(UpdateForm)
ant4.0 需在组件添加formRef = React.createRef()等;(改动如下:)
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Form, Select, Input } from 'antd'
const Item = Form.Item
class UpdateForm extends Component {
formRef = React.createRef();
static propTypes = {
categoryName:PropTypes.string.isRequired
}
componentDidUpdate() {
this.formRef.current.setFieldsValue({
categoryName: this.props.categoryName,
});
}
render() {
const { categoryName } = this.props
return (
<Form ref={this.formRef} >
<Item name='categoryName' initialValue={categoryName} >
<Input placeholder='请输入分类名称' ></Input>
</Item>
</Form>
)
}
}
export default UpdateForm