作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的代码片段
<Table selectable onRowSelection={this.onRecordSelected} bodyStyle={tableBodyStyle}>
<TableBody deselectOnClickaway={false} showRowHover displayRowCheckbox={false}>
{this.props.taskRecords.map((row, index) => (
<TableRow key={row.get('id')} selected={this.state.selectedRecordRowId === index}>
<TableRowColumn>{row.getIn(['task', 'name'])}</TableRowColumn>
<TableRowColumn>{row.getIn(['task', 'description'])}</TableRowColumn>
<TableRowColumn>{row.get('status')}</TableRowColumn>
<TableRowColumn>{row.get('log')}</TableRowColumn>
</TableRow>
))}
</TableBody>
</Table>
我查看了TableRow
和TableRowColumn
的源码,发现它们没有实现shouldComponentUpdate
方法。我知道当任何状态改变时它们都会被重新渲染。
我尝试使用 recompose
库中的 pure
函数将它们纯化为组件。
const TableRow = pure(require('material-ui/lib/table/table-row'));
const TableRowColumn = pure(require('material-ui/lib/table/table-row-column'));
在我的理解中,如果给定的 props
没有改变,净化后的组件将不会再被重新渲染。在这种情况下,所有 Prop 都是来自 Immutable.js
的不可变对象(immutable对象)或函数。
有什么方法可以防止它在我选择任何行时重新呈现吗?
提前谢谢你。
最佳答案
终于找到了解决办法。
<Table selectable onRowSelection={this.onRecordSelected} bodyStyle={tableBodyStyle}>
<TableBody deselectOnClickaway={false} showRowHover displayRowCheckbox={false}>
{this.props.taskRecords.map((row, index) => (
<PureRecordRow key={row.get('id')} selected={this.state.selectedRecordRowId === index}>
<TableRowColumn>{row.getIn(['task', 'name'])}</TableRowColumn>
<TableRowColumn>{row.getIn(['task', 'description'])}</TableRowColumn>
<TableRowColumn>{row.get('status')}</TableRowColumn>
<TableRowColumn>{row.get('log')}</TableRowColumn>
</PureRecordRow>
))}
</TableBody>
</Table>
我创建了一个新组件 PureRecordRow
,它使用自定义 shouldComponentUpdate
方法扩展了 TableRow
:
class PureRecordRow extends TableRow{
constructor(props, context){
super(props, context)
}
shouldComponentUpdate(nextProps, nextState) {
return this.state.hovered !== nextState.hovered || this.props.selected !== nextProps.selected;
}
}
关于javascript - 每次使用 React 和 material-ui 选择一行时,所有 TableRows 都会重新呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37020494/
我是一名优秀的程序员,十分优秀!