简体   繁体   English

与状态问题的 Antd 组件模态表单反应

[英]React with Antd Component Modal Form with state issue

I can't figure out what is the right way to handle this -> I am beginner in React , so if this looks absurd, maybe I got this wrong.我不知道处理这个问题的正确方法是什么 -> 我是 React 的初学者,所以如果这看起来很荒谬,也许我错了。

I have a React component - which is a table/list with a list of Tasks我有一个 React 组件——它是一个包含任务列表的表/列表


  showModal = () => {
    this.setState({
      visible: true,
    });
  };
.
render() {
return ( 
<Table />

        <Button type="primary" onClick={this.showModal}>
          Add New Task
        </Button>
<CRTaskForm
        visible={this.state.visible} />

I have another React component CRTaskForm - which is basically a Modal Form to add Tasks我有另一个 React 组件 CRTaskForm - 它基本上是一个添加任务的模态表单

Now, my issue is 1. I want to handle the onSubmit in CRTaskForm - and close the form when the Task is created - there is a separate handler in CRTaskForm onSubmit(e) { .. } which handles sending the create task via REST to the server.现在,我的问题是 1. 我想处理 CRTaskForm 中的 onSubmit - 并在创建任务时关闭表单 - CRTaskForm onSubmit(e) { .. } 中有一个单独的处理程序,它处理通过 REST 将创建任务发送到服务器。

How do I close the Modal form in the onSubmit .. the "visible" props is not present in the CRTaskForm.如何关闭 onSubmit 中的 Modal 表单..CRTaskForm 中不存在“可见”道具。

I would create a function which closes the modal and pass it to the form as a prop:我将创建一个关闭模态并将其作为道具传递给表单的函数:

closeModal = () => {
  this.setState({
   visible: false
})
}

...

<CRTaskForm visible={this.state.visible} closeModal={this.closeModal}/>

And just call it in your submit function.只需在您的提交函数中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM