虚拟列表中的(仅新的)元素的动画输入

发布于 2025-01-31 02:55:04 字数 1602 浏览 3 评论 0原文

我正在使用react-window filestsizedlistreact-virtualized-auto-sizer autosizer autoSizer 组件构建list/list/list/list/表UI元素可能包含数千个项目,这些项目也通过WebSocket连接接收新项目并将其预先列入列表。我现在需要对此列表中的新元素的输入进行动画

这是一个具有最小(并且不够有效的)示例的代码和框链接: noreflow noreferrer“> codesandbox

请注意,。还要注意

我知道发生这种情况的原因是因为列表如何使用孩子行的绝对定位来实现虚拟化。每次将新项目插入数据或滚动列表时,react-window都需要重新计算每一行的样式 prop,从而重新创建每个DOM元素因此,重新启动.ROW动画。

我想仅动画新的

  1. >到.ROW类。由于上述每个元素都在新项目插件上重新创建,因此这与上述相同的原因不起作用。
  2. 仅动画第一行(示例使用代码沙箱中的红色背景)。虽然“有效”,但它并不十分合适。在我的生产站点中,不保证一次更新一次 - 可能同时插入多行。将所有新行添加到DOM时,都应将它们动画。可以通过在挂钩中立即插入两个UUID来在代码沙箱中复制这一点。
  3. 不使用列表虚拟化。这当然可以正常工作,但不合适。在我的生产站点中,此列表可能包含数千个项目。
  4. 阅读以前的问题。上一个问题的信息很少,没有最小的示例,也没有有用的答案或评论。此外,它已经超过5年了。

如何实现我在这里寻找的结果?

编辑:

我尝试了进一步的尝试,在2)上进行了延伸:

  • 保存每个渲染项目的“旧”列表的副本。收到更新后,从新列表的长度中减去旧列表的长度(调用此号码n)。动画新列表的顶部n项目。此“有效”,但是生产系统具有一些复杂的解决方案,使该解决方案不足 - 每个列表项目都有ISO时间戳的日期,并且根据时间戳new -&gt对列表进行排序;老的。通过WebSocket收到的更新还具有时间戳,,但不能保证新项目总是比当前列表的最新产品更新 - 在某些情况下,将新项目插入2、3或更远的位置列表。在这种情况下,基于长度更改的顶部n项目将不准确。

I am using react-window FixedSizedList and react-virtualized-auto-sizer Autosizer components to build a list/table UI element that may contain thousands of items that also receives new items via a websocket connection and prepends them to the list. I now have a requirement to animate the entry of new elements in this list.

Here is a codesandbox link with a minimal (and not quite working) example: codesandbox.

Note how the .row animation is triggered for every child of FixedSizedList each time the data list receives a new item. Also note how the .row animation is again triggered for every child of FixedSizedList when the list is scrolled.

I understand that the reason this is happening is because of how list virtualization works using absolute positioning of the children rows. Every time a new item is inserted into data, or the list is scrolled, react-window needs to re-compute the style prop for each row, which recreates every DOM element and hence re-triggers the .row animation.

I would like to animate only the new .row DOM elements when they appear.

Things I have tried:

  1. Add animation-iteration-count: 1; to the .row class. This doesn't work for the same reason mentioned above as every element is re-created on a new item insert.
  2. Animate only the first row (example using red background in the code sandbox). While this "works", it's not quite suitable. In my production site, updates are not guaranteed to come through one at a time - multiple rows might be inserted at the same time. All new rows should be animated when they are added to the DOM. This can be replicated in the code sandbox by inserting two UUID's at once in the hook.
  3. Not use list virtualization. This of course works fine, but is not suitable. In my production site this list could contain thousands of items.
  4. Reading this previous question. The previous question is sparse of information, does not have a minimal example, and has no useful answers or comments. Additionally, it is over 5 years old.

How is it possible to achieve the result I'm looking for here?

EDIT:

A further attempt I have tried, extending on 2) above:

  • Save a copy of the "old" list of items each render. When an update is received, subtract the length of the old list from the length of the new list (call this number n). Animate the top n items of the new list. This "works", but the production system has some intricacies making this solution insufficient - each list item is dated with an ISO timestamp, and the list is sorted according to timestamp new -> old. Updates received via websocket also have a timestamp, but there is no guarantee the new item will always be newer than the current top of the list - in some cases, new items are inserted into position 2, 3 or further down in the list. In this case, animating the top n items based on the length change would not be accurate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2025-02-07 02:55:04

不知道对您来说有多昂贵,但是在每个状态更改上,一种做到这一点的方法是将特定的className添加到新的插入行和className已经插入的行。

因此,这样您就可以使用与新插入线相关的className处理。

类似的东西:

const mappedObject = (data, className) => {
  return {
    item: data,
    className
  };
};

React.useEffect(() => {
    const interval = setInterval(() => {
      const alreadyInsertedData = data.map((item) => {
        item.className = "already-inserted";

        return item;
      });

      setData((prev) => [
        mappedObject(uuidv4(), "first-insert"),
        mappedObject(uuidv4(), "first-insert"),
        mappedObject(uuidv4(), "first-insert"),
        ...alreadyInsertedData
      ]);
    }, 3000);

    return () => {
      clearTimeout(interval);
    };
  }, [setData, data]);

这是代码示例向您检查。

Don´t know how costly it can be for you, but one way to do that is on every state change add a specific className to the new insert rows and a className to the already inserted rows.

So that way you can handle using the className related to the new inserted lines.

Something like this:

const mappedObject = (data, className) => {
  return {
    item: data,
    className
  };
};

React.useEffect(() => {
    const interval = setInterval(() => {
      const alreadyInsertedData = data.map((item) => {
        item.className = "already-inserted";

        return item;
      });

      setData((prev) => [
        mappedObject(uuidv4(), "first-insert"),
        mappedObject(uuidv4(), "first-insert"),
        mappedObject(uuidv4(), "first-insert"),
        ...alreadyInsertedData
      ]);
    }, 3000);

    return () => {
      clearTimeout(interval);
    };
  }, [setData, data]);

Here's a code sample to you check.

晨曦÷微暖 2025-02-07 02:55:04

保存每个渲染项目的“旧”列表的副本。收到更新后,从新列表的长度(调用此号码n)中减去旧列表的长度。动画新列表的顶部n个项目。

如您所说,如果列表项目具有唯一的ID,则您只需要在每次更新时比较ID的更新,并对与这些添加或减少ID相对应的列表项进行动画。

Save a copy of the "old" list of items each render. When an update is received, subtract the length of the old list from the length of the new list (call this number n). Animate the top n items of the new list.

As you said, if the list items have a unique id, then you only need to compare the update of the ids each time you update, and perform animation on the list items corresponding to these added or decreased ids.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文