reactjs - 为什么 destroyOnClose={true} 在 React 中不起作用

标签 reactjs typescript react-hooks antd ant-design-pro

我正在使用 TypeScript 开发基于 React hook 的功能应用程序,并且我正在使用 ant design 中的模态。我正在通过模态提交表格表格。因此,模态将被多次调用以填充表格的不同行。
问题是,当第二次、第三次或横向出现模态时,它总是带有以前的值。
为了避免这种情况,我设置了模态 EnableViewState="false" ,没用。我设置destroyOnClose={true} .它没有用。在 modal documentation ,它是在 destroyOnClose 不起作用时写入的,然后我们需要使用 .但是在哪里定义呢?因为,当我设置为,<Form onSubmit={props.inputSubmit} preserve={false}在我的模态表单中,我收到一条错误消息 Type '{ children: Element[]; onSubmit: any; preserve: boolean; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Form>......?你用什么使每次模式重新加载时,它重新加载为空?我不想在输入的表单值字段中分配状态。是否还有其他选项,例如 destroyOnClose={true} ?
这是我的模态,

<Form onSubmit={props.inputSubmit}>
  <Row>
    <Col span={10}>
      <Form.Item>
        <Text strong={true}>Article name: </Text>
      </Form.Item>
    </Col>
    <Col span={12}>
      <Form.Item>
        <Input
          style={{ backgroundColor: '#e6f9ff' }}
          name="articleName"
          onChange={props.handleArticleModalInput}
        />
      </Form.Item>
    </Col>
  </Row>
</Form>
这是调用模态的地方,
return (
  <>
    <ArticleTableModal
      destroyOnClose={true}
      isVisible={modalVisibilty}
      inputSubmit={inputSubmit}
      handleCancel={handleCancel}
      filledData={fetchedData}
      articleNumber={articleNumber}
      handleArticleModalInput={handleArticleModalInput}

    />

    <Table
      pagination={false}
      dataSource={articleDataSource}
      columns={articleColumns}
      scroll={{ y: 400 }}
      bordered
    />
  </>
)
任何帮助深表感谢。

最佳答案

您需要在每次模式启动时为表单中的字段生成动态键。
这是一个可以玩的沙盒。如果您不对键进行任何更改,模态将保留其中的值。如果您更改键并启动模态,则该值将被清除。
Sandbox Link

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Modal, Button, Input } from "antd";

class App extends React.Component {
  state = { visible: false, theKey: "dummy" };

  showModal = () => {
    this.setState({
      visible: true
    });
  };

  handleOk = (e) => {
    console.log(e);
    this.setState({
      visible: false
    });
  };

  handleCancel = (e) => {
    console.log(e);
    this.setState({
      visible: false
    });
  };

  handleChange = ({ target: { value } }) => {
    this.setState({ theKey: value });
  };

  render() {
    return (
      <>
        <Input onChange={this.handleChange} placeholder="key for input field"/>
        <br />
        <Button type="primary" onClick={this.showModal}>
          Open Modal
        </Button>
        <Modal
          title="Basic Modal"
          visible={this.state.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        >
          <Input
            key={this.state.theKey}
            style={{ backgroundColor: "#e6f9ff" }}
            name="articleName"
          />
        </Modal>
      </>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("container"));

关于reactjs - 为什么 destroyOnClose={true} 在 React 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63777412/

相关文章:

css - 如何使行通过不影响列表中的整个文本

javascript - 使用 lodash 的 typescript 字符串插值

javascript - 只有在方法返回 promise 完成后才调用

javascript - react 空闲定时器: Video Player Activity Detected as Idle

javascript - 为什么 React Router 不能与 <Switch> 一起工作?

javascript - 如何在 React 中为对象的对象定义 Prop 验证规则?

typescript - 如何在 TypeScript 中将参数的默认值设置为空数组?

javascript - 在非 React 组件中使用钩子(Hook)的替代方法是什么?

javascript - 在 react 中的两个组件之间共享数据。无亲子关系

javascript - 如何在使用 isomorphic-fetch 进行异常处理 promise 后解析 json