flash-list: Flashlist horizontal wrong height item

Current behavior

Hi, first of all, you lib are cool, but I’m facing with issue, when I try use it in horizontal mode. Item of list are cutting in bottom Снимок экрана 2023-07-24 в 10 29 33

https://github.com/Shopify/flash-list/assets/23406402/30ef17a5-28fd-4dab-90a7-9e2c574deb8d

code:

export const { width: WIDTH, height: HEIGHT } = Dimensions.get('window');

cont Comp = (props) => {
if({!data?.length) return <></>

return (
<FlashList
      ref={ref}
     data={data}
     renderItem=(({ item }) => <Card width={cardWidth} {...item} />)
      renderScrollComponent={ScrollView}
      bounces
     estimatedItemSize={WIDTH}
      showsVerticalScrollIndicator={false}
      showsHorizontalScrollIndicator={false}
      scrollEventThrottle={32}
      onEndReachedThreshold={2}
      {...props}
    />
)
}

Expected behavior

Correct item size without cutting

To Reproduce

Try to add different height for items

Platform:

  • [x ] iOS
  • [ x] Android

Environment

“react-native”: “0.71.8”, “@shopify/flash-list”: “^1.4.2”

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 21

Most upvoted comments

I’m also struggling with the same issue.

Here is a repro: https://snack.expo.dev/@mateuszrostkowski/flashlist-screen-orientation-switch-bug

I’ve got problem after changing orientation the item size is cut in the middle.

https://github.com/Shopify/flash-list/assets/38006370/767c1d11-9c58-4965-af28-d67de01ec82a

@dan-pugsley @davidcognite interesting!

I very much appreciate you putting your solutions here.

I’ll be revisiting this issue to see if either of those resolve the issue as both of those sound relevant to my scenario.

@carlos-dubon this is all cool and interesting and looks great, but it would be good if this was fixed inside the plugin, otherwise it has been weighing for more than a year 😃

This fixed the problem for me. I recommend first finding what is your avg. item height. The only downside is it shifts the layout on initial render…

import {FlashList, FlashListProps} from '@shopify/flash-list';
import {useEffect, useRef, useState} from 'react';
import {View} from 'react-native';

interface HorizontalFlashListComponentProps<T> extends FlashListProps<T> {
  estimatedItemHeight: number;
}

export const HorizontalFlashListComponent = <T extends unknown>(
  props: HorizontalFlashListComponentProps<T>,
) => {
  const [maxItemHeight, setMaxItemHeight] = useState<number>(
    props.estimatedItemHeight,
  );

  const flashListRef = useRef<FlashList<T>>(null);

  // FIXME: See https://github.com/Shopify/flash-list/issues/881
  useEffect(() => {
    // @ts-ignore: Bypass private property access warning
    flashListRef.current?.rlvRef?._onSizeChanged({
      // @ts-ignore: Bypass private property access warning
      width: flashListRef.current.rlvRef._layout.width,
      height: maxItemHeight,
    });
  }, [maxItemHeight]);

  return (
    <FlashList
      {...props}
      ref={flashListRef}
      horizontal
      renderItem={listItem => {
        return (
          <View
            onLayout={layoutEvent => {
              const itemHeight = layoutEvent.nativeEvent.layout.height;

              if (itemHeight > maxItemHeight) {
                setMaxItemHeight(itemHeight);
              }
            }}>
            {props.renderItem?.(listItem)}
          </View>
        );
      }}
    />
  );
};

I’ve been experiencing FlashList’s height only ever updating when the new height is smaller. I had a poke around, and onLayout will follow this trend of only firing when the new height is smaller. So I dug a bit deeper in to the RecyclingListView that FlashList effectively wraps, and I think I’ve found a workaround for this, if you know the desired height:

useEffect(() => {
	YourFlashListRef.current.rlvRef?._onSizeChanged({
		width: YourFlashListRef.current.rlvRef._layout.width,
		height: desiredHeight
	})
}, [desiredHeight, YourFlashListRef])

I don’t know if the issue lies with RecyclingListView rather than FlashList, but I hope this helps with any further investigation.

@VictorPulzz yeah, I agree. Let’s hope they don’t close the issue at least. (Next.js likes to do that when community solutions are proposed)

For me the issue was that my EmptyListComponent height was smaller than my renderItem height. As soon as I set the EmptyListComponent to the same height as my items everything worked as expected.

I’ve been experiencing FlashList’s height only ever updating when the new height is smaller. I had a poke around, and onLayout will follow this trend of only firing when the new height is smaller. So I dug a bit deeper in to the RecyclingListView that FlashList effectively wraps, and I think I’ve found a workaround for this, if you know the desired height:

useEffect(() => {
	YourFlashListRef.current.rlvRef?._onSizeChanged({
		width: YourFlashListRef.current.rlvRef._layout.width,
		height: desiredHeight
	})
}, [desiredHeight, YourFlashListRef])

I don’t know if the issue lies with RecyclingListView rather than FlashList, but I hope this helps with any further investigation.

Thanks for this, it’s worked for me. My only issue is I don’t want to use a fixed height for my list as my data usually determines the height. Any idea the best way to calculate what the correct height should be?