React之ref的使用

我想用ref引用Ant Design的表单组件,从而实现表单的自动提交。可以使用ref实现。

<Form onSubmit={this.handleSubmit} ref={a=>{this.formRef = a}}>
componentDidMount() {
    console.log('formRef',this.formRef)
    this.formRef.props.onSubmit()
}

上面的代码即是将表单的ref设置为this.formRef,然后在componentDidMount中触发自动提交。

即可。

posted @ 2021-12-30 10:22  罗毅豪  阅读(204)  评论(0编辑  收藏  举报