React Virtualized를 사용하여 채팅 응용 프로그램을 만들려고합니다. 지금까지 모든 것이 작동하지만 목록에서 keyMapper와 rowRenderer를 사용할 때 잘못된 생각을하고 있다고 생각합니다.React Virtualized CellMeasurer를 사용하여 목록 및 동적 높이의 데이터 순서 변경하기
캐시는 ID를 사용하여 _rowHeightCache에 높이를 저장하지만 높이는 ID가 아닌 인덱스를 사용하여 조회 한 것으로 보입니다. 만약 내가 id를 rowIndex로 CellMeasurer에게 넘겨서 그 고질의 것을 얻으려면 확실하지 않다.
문제는 메시지 목록의 순서가 변경되어 높이가 잘못되어 색인의 높이가 적절하지 않으며 메시지를 제거하여 색인 주문을 엉망으로 만들 수 있다는 것입니다. 이것은 높이를 찾기 위해 keyMapper를 사용하여 고쳐야한다고 생각하지만 잘못하고 있어야합니다.
다음은 내가 사용하는 방법에 대한 편집 된 다운 버전입니다.
constructor(props) {
this._cache = new CellMeasurerCache({
defaultHeight: 45,
fixedWidth: true,
keyMapper: (index) => _.get(this.props.messageList[index], ['msgId'])
});
}
render() {
const props = this.filterProps(this.props),
list = this.props.messageList,
rowCount = list.length;
return (
<InfiniteLoader
threshold={0}
isRowLoaded={this._isRowLoaded}
loadMoreRows={this._loadMoreRows}
rowCount={rowCount}>
{({onRowsRendered, registerChild}) => (
<AutoSizer>
{({width, height}) => (
<List
ref={(ref) => {
this.streamLane = ref;
registerChild(ref);
}}
height={height}
width={width}
onRowsRendered={onRowsRendered}
rowCount={rowCount}
rowRenderer={this._rowRenderer}
deferredMeasurementCache={this._cache}
rowHeight={this._cache.rowHeight}
/>
)}
</AutoSizer>
)}
</InfiniteLoader>
);
}
_rowRenderer({ index, key, parent, style }) {
return (
<CellMeasurer
cache={this._cache}
columnIndex={0}
key={key}
parent={parent}
rowIndex={index}>
<div style={{...style}}>
{this.props.messageList[index]}
</div>
</CellMeasurer>
);
}
_loadMoreRows({startIndex, stopIndex}) {
return new Promise((resolve) => {
if (!!stopIndex || !!startIndex || this.props.hasLastMessage || this.state.isPinned) {
return resolve();
}
this.props.loadOlder(() => resolve);
});
}
_isRowLoaded({index}) {
if (index === 0) return false;
return !!this.props.messageList[index];
}
}
어떤 도움, 제안 또는 비판도 놀라운 것입니다.
@brianvaughn 불편을 끼쳐 드려 죄송합니다. 시간이 없다면 걱정할 필요가 없습니다. 감사! – ReganPerkins