提问人:Naruto 提问时间:7/5/2023 最后编辑:Geeky QuentinNaruto 更新时间:7/11/2023 访问量:327
react-window spacing between items 列表
react-window spacing between items list
问:
我正在尝试将 react-window 与 react-window-infinite-loader 集成,到目前为止,我已经成功了,但面临一个问题,即我无法找到一种方法来设置列表项的样式以在列表项之间创建更多间距。
这是我下面的代码
export default function AutoComplete({}: AutoCompleteProps) {
//testdata length is 35000
const LOADING = 1;
const LOADED = 2;
const isItemLoaded = (index) => testData[index]?.loading === LOADED;
const loadMoreItems = (startIndex, stopIndex) => {
for (let index = startIndex; index <= stopIndex; index++) {
testData[index].loading = LOADING;
}
return new Promise<void>((resolve) =>
setTimeout(() => {
for (let index = startIndex; index <= stopIndex; index++) {
testData[index].loading = LOADED;
}
resolve();
}, 2500)
);
};
const Row = ({ index, style }) => {
let label;
if (testData[index].loading === LOADED) {
label = `${testData[index].title}`;
} else {
label = "Loading...";
}
const listStyle = { ...style };
return (
<div
className={`whitespace-nowrap overflow-hidden text-ellipsis`}
style={style}
>
<div>{label}</div>
</div>
);
};
return (
<InfiniteLoader
isItemLoaded={isItemLoaded}
itemCount={testData.length}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
style={{ position: "absolute", width: "100%", marginTop: 50 }}
className="absolute top-[110%] z-50 bg-white w-full"
height={250 - 40}
itemCount={testData.length}
itemSize={30}
width={300}
onItemsRendered={onItemsRendered}
ref={ref}
>
{({ index, style }) => <Row index={index} style={style} />}
</List>
)}
</InfiniteLoader>
);
}
答:
1赞
UNRIVALLEDKING
7/8/2023
#1
这是包含所需修复程序的 codesandbox 存储库。该问题是由于您传递的样式道具造成的。
那个风格道具正在添加这些样式,我覆盖了它,在课堂上做了一些小的改动
List and ListItem
您可以更改边距,因为现在感觉会很好。
评论
0赞
Naruto
7/10/2023
这仍然无法正常工作,请尝试将其向下滚动太多,您可能会看到列表项未呈现,这是我在尝试多个示例后面临的相同问题:)
0赞
Naruto
7/10/2023
似乎我们无法更改“top”属性
0赞
Suryasish Paul
7/11/2023
在这种情况下,更改顶部不会产生任何影响,因为位置设置为相对位置。Top 或任何其他此类属性仅在位置是某种形式的绝对定位(绝对本身或粘性/固定/等)时生效。
0赞
UNRIVALLEDKING
7/11/2023
@Naruto我滚动到最后。它渲染正确。这是图片
0赞
Naruto
7/12/2023
奇怪的不为我渲染
2赞
Suryasish Paul
7/11/2023
#2
增加道具的价值,并按比例增加道具的价值将解决问题。这里有一个沙盒供您参考:https://codesandbox.io/s/cold-glade-hcgy8q?file=/src/App.js<List></List>
height
itemSize
此外,您可以在此处阅读文档中提到的道具: https://react-window.vercel.app/#/api/FixedSizeList
祝你好运!
评论