gpt4 book ai didi

react-virtualized scrollToIndex 不适用于 List

转载 作者:行者123 更新时间:2023-12-03 18:14:50 40 4
gpt4 key购买 nike

尝试 scrollToIndex 时,屏幕变为空白。

这是一个重现问题的代码和框。只需在顶部的输入框中输入 100,然后单击“滚动到行”按钮。
https://codesandbox.io/s/zr2r2wp1x

这是一个有点复杂的 View - 它使用 InfiniteLoader、WindowScroller、Autosizer、CellMeasurer、CellMeasurerCache 和 List。

这是代码:

import React, { Component } from 'react';
import {
AutoSizer,
CellMeasurer,
CellMeasurerCache,
InfiniteLoader,
List,
WindowScroller
} from 'react-virtualized';


const cache = new CellMeasurerCache({
fixedWidth: true,
defaultHeight: 100
});

class Hello extends Component {
constructor(props) {
super(props);
this.state = {
scrollToIndex: undefined
}
this.list = [];
this.makeList = this.makeList.bind(this);
this.makeList();
this.handleClick = this.handleClick.bind(this);
}

makeList() {
for (let i = 0; i < 200; i++) {
this.list.push("yo");
}
}


handleClick() {
this.setState({
scrollToIndex:this.inputRow.value
})
}

isRowLoaded({ index }) {
return !!this.list[index];
}

loadMoreRows({ startIndex, stopIndex }) {
this.makeList();
}

rowRenderer({ index, isScrolling, isVisible, key, parent, style }) {
if (isScrolling) {
return null;
}

if (isVisible) {
return (
<CellMeasurer
key={key}
cache={cache}
parent={parent}
columnIndex={0}
rowIndex={index}
>
<div style={style}>
this is row: {index}
</div>
</CellMeasurer>
);
}
return null;
}


render() {

if (this.list.length === 0) {
return <div>No data available.</div>;
}

const scrollToIndex = this.state.scrollToIndex;

return (
<div>
<div>
<input
type="number"
ref={(input) => { this.inputRow = input; }} />
<input
type="button"
value="Scroll To Row"
onClick={this.handleClick}
/>
</div>
<InfiniteLoader
isRowLoaded={isRowLoaded => this.isRowLoaded(isRowLoaded)}
loadMoreRows={loadMoreRows => this.loadMoreRows(loadMoreRows)}
rowCount={10000}
threshold={1}
>
{({ onRowsRendered, registerChild }) => (
<WindowScroller>
{({ height, isScrolling, onChildScroll, scrollTop }) => (
<AutoSizer disableHeight>
{({ width }) => {
console.log("scroll to index", scrollToIndex)
return (
<List
autoHeight
height={height}
onRowsRendered={onRowsRendered}
ref={registerChild}
width={width}
rowCount={this.list.length}
rowHeight={cache.rowHeight}
rowRenderer={rowRenderer => this.rowRenderer(rowRenderer)}
scrollTop={scrollTop}
deferredMeasurementCache={cache}
style={{ paddingLeft: '0', paddingRight: '0' }}
scrollToAlignment="start"
scrollToIndex={scrollToIndex}
/>
)
}}
</AutoSizer>
)}
</WindowScroller>
)}
</InfiniteLoader>
</div>
);
}
}

export default Hello;

最佳答案

这是一个 updated CodeSandbox 显示如何完成您想要的行为。我必须解决您的入门代码的许多问题才能使示例正常工作 - 简短的回答是您缺少将 WindowScroller 的渲染 Prop onChildScroll 传递给 List 组件的 onScroll Prop 。此设置将允许两个组件相互通信以有效地正确更新列表。

关于react-virtualized scrollToIndex 不适用于 List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49431819/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com