可能不是什么大问题,除非你注意到大数据集会让事情变得滞后。说到表现,如果你觉得自己有问题,就先衡量一下(
read more on measuring React performance
)不要浪费时间试图过度优化,直到你知道你有问题。
曾经有人建议把每一行分成
PureComponent
:
import React, { PureComponent } 'react';
class AlbumTableRow extends PureComponent {
render() {
const {
key,
artist,
dob,
country
albums,
albumsHiddenStatus,
onToggleVisibility,
} = this.props;
return(
<Table.Body key={key}>
<Table.Row >
<Table.Cell>{artist}</Table.Cell>
<Table.Cell>{dob}</Table.Cell>
<Table.Cell onClick={onToggleVisibility}>+</Table.Cell>
<Table.Cell>{country}</Table.Cell>
</Table.Row>
<AlbumRow result={albums} hiddenStatus={albumsHiddenStatus} />
</Table.Body>
);
}
}
export default AlbumTableRow;
纯组分
应该
只有当道具
价值观
改变。然后您应该更改父组件代码,如下所示:
getOnToggleVisibility = (index) => () => this.toggleVisibility(index);
renderResult(){
let rows = this.props.artists.map((r, index) =>{
return (
<AlbumTableRow
key={index}
artist={r.artist}
dob={r.dob}
country={r.country}
albums={r.albums}
albumsHiddenStatus={!this.state.AlbumStatus[index]}
onToggleVisibility={this.getOnToggleVisibility(index)}
/>
);
});
return rows;
}
注意,我添加了
getOnToggleVisibility
方法,它接受该索引并返回
onToggleVisibility
知道要使用的正确索引的函数。