How to submit form from a button outside that component in React?

后端 未结 6 1000
野趣味
野趣味 2020-12-31 02:07

I have a form in one of my React components, and and in the outside component that calls it I want to pass a reference to a button there, so that I can also submit that usin

6条回答
  •  庸人自扰
    2020-12-31 02:32

    You can set in the state a flag propery for example rootBtnClicked: false and a method that handles the click in the App component. Also in the , add a prop e.g. rootBtnClicked={this.state.rootBtnClicked}. When "In Root" button is clicked, trigger that method; then the method changes the state with setState(). Then inside the CustomForm component's render() method, check if the prop is true. If it is, manually trigger .submit() using React's ref.

提交回复
热议问题