我正在尝试将 Table.row
的 ref
传递到点击处理程序中。也许我想选择一行并在通过更改类名选择时突出显示它。但是它总是记录 null。
尝试:
<Table.Body>
{_.map(data, ({ Name, Version, Type, Modified }) => (
<Table.Row
ref={(ref) => { this.row = ref; }}
className=""
key={Version}
onClick={() => this.handleClick(this.row, Name, Version, Type, Modified)}
>
<Table.Cell>{Name}</Table.Cell>
<Table.Cell>{Version}</Table.Cell>
<Table.Cell>{Type}</Table.Cell>
<Table.Cell>{Modified}</Table.Cell>
</Table.Row>
))}
</Table.Body>
handleClick(row, Name, Version, Type, Modified) {
const me = this;
log.info('logging row', row); //null
log.info('logging row data', Name, Version, Type, Modified); //works
}
有人知道为什么ref
在这里不能正常工作吗?
最佳答案
正如我在评论中提到的,我在docs中没有看到任何对引用的支持。 .
至于您的 ref 实现,您将在每次迭代时重写 this.row
。
无论如何,我认为有一个更 react 的解决方案。
如果您想跟踪选择了哪些行,您将需要一个在您的状态中的对象,该对象将在每次单击行时更新,这是一种查找表对象。
要创建这样的查找表,每行都需要 id(可以使用数据对象中的 id 或数组的索引)。
为了能够将 id
传递到 Table.Row
并作为 onClick
的参数返回到父级您可以使用 class
组件包装 Table.Row
,该组件将处理点击及其返回的数据。
例如,MyRow
将期望获得一个 rowId
、一个 onClick
事件,当然还有 active
属性我们将把它传递给Table.Row
。
当MyRow
被点击时,它会将this.props.rowId
作为参数传递给父级,父级将更新查找表。
以下是此用例的运行示例:
const { Table } = semanticUIReact; // --> import { Table } from 'semantic-ui-react'
const usersFromServer = [
{ name: 'john', age: 25, gender: 'male', id: 1 },
{ name: 'jane', age: 22, gender: 'female', id: 2 },
{ name: 'david', age: 31, gender: 'male', id: 3 },
{ name: 'jain', age: 29, gender: 'female', id: 4 }
];
class MyRow extends React.Component {
onClick = (e) => {
const { onClick, rowId } = this.props;
onClick(rowId, e);
}
render() {
const { data, active } = this.props;
return (
<Table.Row onClick={this.onClick} active={active}>
<Table.Cell>{data.name}</Table.Cell>
<Table.Cell>{data.age}</Table.Cell>
<Table.Cell >{data.gender}</Table.Cell>
</Table.Row>
);
}
}
class TableExample extends React.Component {
constructor(props) {
super(props);
this.state = {
activeRows: []
}
}
onRowClick = (id, e) => {
const { activeRows } = this.state;
const nextRows = {
...activeRows,
[id]: !activeRows[id]
}
this.setState({ activeRows: nextRows });
}
render() {
const { activeRows } = this.state;
return (
<Table unstackable>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Name</Table.HeaderCell>
<Table.HeaderCell>Age</Table.HeaderCell>
<Table.HeaderCell>Gender</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{
usersFromServer.map((u) => {
const isActive = activeRows[u.id];
return (
<MyRow
active={isActive}
key={u.id}
rowId={u.id}
data={u}
onClick={this.onRowClick}
/>
);
})
}
</Table.Body>
</Table>
)
}
}
ReactDOM.render(<TableExample />, document.getElementById('root'));
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44372129252a302d2769312d69362125273004746a73736a76" rel="noreferrer noopener nofollow">[email protected]</a>/dist/umd/semantic-ui-react.min.js"></script>
<div id="root"></div>
关于javascript - ref 为 null - 语义 UI React 表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290915/