【发布时间】:2021-02-22 15:04:14
【问题描述】:
我正在使用 Material UI Table 组件。
在 Table 组件中,我想将 EnhancedTableHead 中的选中属性存储到我的父组件 EventManagement。
但我不确定正确的方法是什么,因为我需要从中提取事件对象,以获取选中的道具
App.js
export default class EventManagement extends Component {
state = {
// How to set the below state by refering the table checkbox
isSelectAllCheckboxSelected: false
};
render() {
return <Table />;
}
}
Demo.js(表格组件)
function EnhancedTableHead(props) {
...
return (
<TableHead>
<TableRow>
<TableCell padding="checkbox">
{/* How to get the checked prop below and send it to parent component */}
<Checkbox
indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount}
onChange={onSelectAllClick}
inputProps={{ "aria-label": "select all desserts" }}
/>
</TableCell>
</TableRow>
</TableHead>
在这种情况下,由于选中了全选复选框,因此状态 isSelectAllCheckboxSelected 应该为 true。
代码沙盒:
https://codesandbox.io/s/material-demo-forked-hf5z9?file=/demo.js
【问题讨论】:
-
您可以传递一个回调,当您在子组件中执行操作时,该回调将更新父组件的状态。
标签: javascript reactjs