最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-window set item size according to content height - Stack Overflow

programmeradmin1浏览0评论

I'm using react-window to implement a chat message list, but I'm getting stuck trying to set the right itemSize to each item. Happens that a chat message, based on his text length and window width, hasn't always a prefixed height (or a height that i can calculate in a simple way).

I'm currently using a VariableSizeList and the code looks like this

<AutoSizer>
    {({ height, width }) => (
       <List
        height={height}
        itemCount={messages.length}
        itemSize={(index) => messages[index].isReply ? 118 : 79} /* THIS IS CURRENTLY WRONG, DOESN'T PICK ALL CASES!*/
        width={width}
       >
         {({ index, style }) => (
           <ChatMessage
            key={index}
            style={style}
            ...
           />
         )}
       </List>
    )}

Is there a way to set the item height of the list row equal to the ACTUAL height of its content?

I'm using react-window to implement a chat message list, but I'm getting stuck trying to set the right itemSize to each item. Happens that a chat message, based on his text length and window width, hasn't always a prefixed height (or a height that i can calculate in a simple way).

I'm currently using a VariableSizeList and the code looks like this

<AutoSizer>
    {({ height, width }) => (
       <List
        height={height}
        itemCount={messages.length}
        itemSize={(index) => messages[index].isReply ? 118 : 79} /* THIS IS CURRENTLY WRONG, DOESN'T PICK ALL CASES!*/
        width={width}
       >
         {({ index, style }) => (
           <ChatMessage
            key={index}
            style={style}
            ...
           />
         )}
       </List>
    )}

Is there a way to set the item height of the list row equal to the ACTUAL height of its content?

Share Improve this question asked Nov 24, 2021 at 8:15 Marco NobileMarco Nobile 1811 silver badge4 bronze badges 1
  • Did you ever found a working solution? – Mutu A. Commented Jul 24, 2022 at 17:30
Add a ment  | 

1 Answer 1

Reset to default 2

That's a little tricky and not officially supported by react-window as discussed in this issue. Apparantly one way to trick this is by using Element.getBoundingClientRect().height and setting the size of this Element via itemSize. An example is given here https://codesandbox.io/s/dynamic-size-of-react-window-list-items-64o9p?file=/src/ChatMessage.js

发布评论

评论列表(0)

  1. 暂无评论