Skip to content

Conversation

@rozhkovs
Copy link
Contributor

@rozhkovs rozhkovs commented Mar 7, 2025

This fixes issue #442 (Or a similar problem).

onLayout on Android does not always set the correct value immediately. To fix this, I suggest using a slightly different condition for rendering and setting the size. If the height or width is 0, we assume that the layout has not been calculated yet.

I tested this on a Redmi Note 11E Pro device in the release version. Expo: 52.0.36, RN: 0.77.1

This probably eliminates the effect, not the cause. But since it's difficult to analyze because the bug is floating, I decided to propose such changes.

before:

Screenrecorder-2025-03-07-16-35-39-721.mp4

after:

Screenrecorder-2025-03-07-16-28-56-924.mp4
My test code
export default function AutoSizer({
  children,
}: {
  children: ({ width, height }: WidthAndHeight) => any
}) {
  const [layout, setLayout] = useState<WidthAndHeight | null>(null)
  
  const letLastMessageRef = useRef('')


  const onLayout = useCallback(
    (event: LayoutChangeEvent) => {
      const nl = event.nativeEvent.layout
      // https://github.com/necolas/react-native-web/issues/1704
      if (!layout || layout.width !== nl.width || layout.height !== nl.height) {
        setLayout({ width: nl.width, height: nl.height })
      }
      letLastMessageRef.current += `---- onLayout ----\n`
      letLastMessageRef.current += `l.w ${layout?.width} l.h ${layout?.height} nl.w ${nl.width} nl.h ${nl.height}\n`
      Alert.alert('onLayout', letLastMessageRef.current)
    },
    [layout, setLayout]
  )

  const isLayoutInizialized = layout && layout.height > 0 && layout.width > 0

  return (
    <View
      onLayout={onLayout}
      style={[sharedStyles.overflowHidden, sharedStyles.root, isLayoutInizialized && layout]}
    >
      {isLayoutInizialized ? children(layout) : null}
    </View>
  )
}

@iM-GeeKy
Copy link
Collaborator

Thanks @rozhkovs!

@iM-GeeKy iM-GeeKy merged commit d0d73dd into web-ridge:master Mar 10, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants