【发布时间】:2017-12-09 16:03:58
【问题描述】:
我有一个材料-ui table 我想传递选定的行以在 DELETE 按钮中起作用。
constructor(props) {
super(props);
this.state = {
selectedRows: 'none',
};
}
onRowSelection(val){
console.log(this);
this.setState({
selectedRows: val,
});
}
render() {
<Table
fixedHeader={true}
selectable={true}
multiSelectable={true}
onRowSelection={this.onRowSelection.bind(this)}
>
...
<TableFooter adjustForCheckbox={true}>
<TableRow>
<TableRowColumn colSpan="5" style={{textAlign: 'right'}}>
<RaisedButton
primary={true}
label="DELETE"
labelPosition="after"
icon={<ActionDelete/>}
onClick={this.props.onDelete.bind(this.state.selectedRows)}
/>
</TableRowColumn>
</TableRow>
</TableFooter>
</Table>
}
我想将 this.state.selectedRows 传递给 this.props.onDelete 函数,但是作为 在此issue 中提到,material-ui 在 onRowSelection 中的 setState 存在问题。
我该如何解决它,是否有另一种方法可以访问选定的行并将它们传递给某个函数以及为什么存在此类错误(如果我在父组件上使用 setState,为什么 onRowSelection 会介意?)
【问题讨论】:
-
我认为选择消失的原因是 this.setState() 会重新渲染,与 this.forceUpdate() 效果相同。
标签: javascript reactjs material-design state