每次打开模态时,reactjs componentWillMount都不会调用

时间:2017-11-14 12:50:19

标签: reactjs material-ui

我在模态对话框中显示表格数据,当我点击表格行来编辑数据时,它将打开另一个模态对话框,这里我通过使用componentWillMount()内的点击行ID来获取行数据。但是当我关闭编辑模态对话框并尝试编辑其他表格行componentWillMount()时不会调用。所以编辑模态对话框为空。为什么componentWillMount每次都不打电话?

2 个答案:

答案 0 :(得分:0)

作为Nir H already mentionedDialog组件仅安装到dom中一次,然后在关闭它之后,变为不可见(通过将左侧位置更改为-10000px)但仍然存在于DOM内。 / p>

很明显,componentWillMountcomponentDidMount方法只会调用一次。如果您希望每次都打开具有不同选项的Dialog组件,您可以执行以下操作:

  1. 将ref保存到父组件中的Dialog组件实例:
  2. ref={instance => {this.RowDialog = instance}}

    1. 然后您可以使用handleOpen方法打开Dialog:
    2. onRowClick = (event) => { this.RowDialog.handleOpen(); }

答案 1 :(得分:0)

我使用了以下步骤来修复它。

  1. 我使用了{(this.state.open) ? <Dialog open={true} ></Dialog> : null
  2. 卸载组件componentWillUnmount() { }
  3. 的第二步
相关问题