简体   繁体   English

使用反应虚拟化向下滚动时,树项消失

[英]Tree items dissapearing on scroll down with react-virtualized

recently I have been using react-virtualized library to render my tree item view.最近我一直在使用 react-virtualized 库来渲染我的树项视图。 I have followed example from the docs however I end up having very strange problem with items disappearing when I scroll down.我遵循了文档中的示例,但是当我向下滚动时,我最终遇到了项目消失的非常奇怪的问题。

I have created codesandbox to show this behaviour and code.我创建了代码沙盒来显示这种行为和代码。

https://codesandbox.io/s/bitter-snow-23vci?file=/src/App.js https://codesandbox.io/s/bitter-snow-23vci?file=/src/App.js

Main idea of virtualized list to render it as a list.虚拟化列表的主要思想将其呈现为列表。

If you pass down tree like structure and render it like in your code sample如果您传递树状结构并在代码示例中呈现它

<List
 ....
            rowCount={data.length}
          />

You don't change rowCount value and keep expanded state in your Node component.您不会更改 rowCount 值并在 Node 组件中保持展开状态。

const Node = ({ data, listRef, depth }) => {
  const [isExpanded, setIsExpanded] = React.useState(false);

But then you scroll out of screen your Node element will be destroyed and recreated then you return.但是,当您滚动出屏幕时,您的 Node 元素将被销毁并重新创建,然后您返回。

You need to keep your selections outside of Node element.您需要将您的选择保留在 Node 元素之外。

like喜欢

// [key]: value structure there key is id of element and value [true, false].
const rootObject = {[elementId]: true};
const App = () => {
  const [visibleNodes, setVisibleNodes] = useState(rootObject)

  ....
  <List
           ...
            rowRenderer={({ index, style, key }) => {
              return (
                <Node
                  setVisibleNodes={setVisibleNodes}
                  visibleNodes={visibleNodes}
                  style={style}
                  key={key}
                  data={data[index]}
                  listRef={ref}
                  depth={1}
                />
              );
            }}
            rowCount={data.length}
            width={width}
          />

And in Node而在节点

const Node = ({ data, listRef, depth, setVisibleNodes, visibleNodes }) => {
  const isExpanded = visibleNodes[data.id];
  const handleClick = (e) => {
    if (data.children.length === 0) return;

    e.stopPropagation();
    setVisibleNodes({...visibleNodes, [data.id]: !!isExpanded});
    listRef.current.recomputeRowHeights();
    listRef.current.forceUpdate();
  };

  return (
    <div onClick={handleClick}>
      {data.children.length ? (isExpanded ? "[-]" : "[+]") : ""} {data.name}
      {isExpanded && (
        <div style={{ marginLeft: depth * 15 }}>
          {data.children.map((child, index) => (
            <Node
              key={index}
              data={child}
              listRef={listRef}
              depth={depth + 1}
            />
          ))}
        </div>
      )}
    </div>
  );
};

I think it works)我认为它有效)

But it's better to do such things like real list and make tree hierarchy just visually.但是最好做一些像真实列表这样的事情,并在视觉上使树层次结构。 By that way you'll use Virtualisation List as it was purposed by creators)通过这种方式,您将使用虚拟化列表,因为它是创作者的目的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM