Problem description
- When you pass a parameter to each form entry in the form, the parameter is already passed in, but the initialValue has not changed.
why
This is because of the bad timing of the call to resetFileds, which is a lifecycle issue.
The solution
- Add the following code to the lifecycle function componentDidUpdate.
componentDidUpdate() {
if (this.formRef.current ! = =null) {
this.formRef.current.resetFields(); }}Copy the code
Scene: the code
export default class UpdateForm extends Component {
formRef = React.createRef();
static propTypes = {
categoryName: PropTypes.string.isRequired
}
componentDidUpdate() {
if (this.formRef.current ! = =null) {
this.formRef.current.resetFields(); }}render() {
const { categoryName } = this.props;
console.log(categoryName);
return (
<Form ref={this.formRef}>
<Item initialValue={categoryName} name="categoryName" >
<Input placeholder="Please enter category name" />
</Item>
</Form>)}}Copy the code