saleel / react-native-super-grid

Responsive Grid View for React Native
MIT License
1.43k stars 153 forks source link

style: update types to handle ref attributes #189

Closed leezumstein closed 2 years ago

leezumstein commented 2 years ago

Context

Updating the types for FlatGrid and SectionGrid so we can leverage the ref attribute for the underlying SectionList and FlatList. This allows the consumer to do something like the following:

import * as React from 'react'

import { FlatList, Text } from 'react-native'
import { FlatGrid } from 'react-native-super-grid'

const DATA = [1, 2, 3, 4]

const MyComponent: React.FC = () => {
  const ref = React.useRef<FlatList<number>>(null)

  const onLayout = useCallback(() => {
    if (ref.current == null) return

    ref.current.scrollToEnd({ animated: true })
  }, [])

  return (
    <FlatGrid
      ref={ref}
      onLayout={onLayout}
      itemDimension={100}
      data={DATA}
      renderItem={({ item }) => <Text>{item}</Text>}
    />
  )
}

Without these changes, the components are typed with the ref attribute as FlatGrid<number>, which doesn't expose any of the methods defined by FlatList.

Image of the typing errors we were running into our project with the current implementation:

Screen Shot 2022-07-26 at 9 15 52 PM

Image of the updated types being inferred after these changes are made:

Screen Shot 2022-07-26 at 9 17 14 PM

Changes

saleel commented 2 years ago

Sorry for the delay. Merged and released v4.4.3

leezumstein commented 2 years ago

Sorry for the delay. Merged and released v4.4.3

No worries, thanks for taking a look + releasing!

leezumstein commented 2 years ago

@saleel I just checked and it looks like v4.4.3 hasn't landed on npm just yet. Wasn't sure if an action wasn't kicked off for the new version to publish it?

saleel commented 2 years ago

@leezumstein It was my mistake. I forgot to publish and only pushed to master instead. Published now - 4.4.3

leezumstein commented 2 years ago

@leezumstein It was my mistake. I forgot to publish and only pushed to master instead. Published now - 4.4.3

Yup I see it now, thanks again!