question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FlatList inverted, itemLayoutAnimation and Typescript not working

See original GitHub issue

https://user-images.githubusercontent.com/43195241/146722205-34558cbe-5ffd-4653-b648-e99fd6c0c852.mp4

Description

I found this issue when use Animated.FlatList with inverted. You can see my video below:

Expected behavior

inverted, itemLayoutAnimation and Typescript working

Actual behavior & steps to reproduce

Install library then use Animated.FlatList

Snack or minimal code example

import React, {memo, useCallback, useState} from 'react';
import isEqual from 'react-fast-compare';
import {Button, Text, View} from 'react-native';
import Animated, {Layout} from 'react-native-reanimated';
import {SafeAreaView} from 'react-native-safe-area-context';

const LoginComponent = () => {
  // state
  const [data, setData] = useState<Array<string>>([
    'Hello',
    'Hi',
    'My name is Jon',
  ]);

  // func
  const onPush = useCallback(() => {
    setData(d => ['New Text'].concat(d));
  }, []);

  // render
  return (
    <SafeAreaView style={{flex: 1}}>
      <Animated.FlatList
        inverted
        itemLayoutAnimation={Layout.springify()}
        renderItem={({item, index}) => {
          return (
            <View key={index}>
              <Text>{item}</Text>
            </View>
          );
        }}
        data={data}
      />
      <Button title={'PUSH'} onPress={onPush} />
    </SafeAreaView>
  );
};

export const Login = memo(LoginComponent, isEqual);

Typescript error

image

Package versions

  • React Native: 0.66.4
  • React Native Reanimated: 2.3.1
  • NodeJS: 17.3.0
  • Xcode: 12.5.1

Affected platforms

  • [] Android
  • iOS
  • Web

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
punkstacommented, Jan 31, 2022

Perhaps I am getting the same issue. It’s not typescript, it’s inverted behavior.

expected behavior

https://reactnative.dev/docs/flatlist#inverted

Reverses the direction of scroll. Uses scale transforms of

actual behavior

inverted is mirroring the list elements against X axis. Instead of reversing the direction of scroll.

FlatList

simulator_screenshot_739E8E94-288E-4E2C-9BDB-09EACAC772FF

Animated.FlatList

simulator_screenshot_AD0624A9-9229-4A17-BCAE-D42F39206FD3

I fixed my issue by rotating elements back on X axis.


 export const WeightList = () => {
  const { data } = useWeightHistory()
  return (
    <Animated.FlatList
      style={{
        flex: 1
      }}
      keyExtractor={keyExtractor}
      inverted
      data={data}
      renderItem={info => (
        <Animated.View
          style={{
            transform: [
              {
                rotateX: '180deg'
              }
            ]
          }}
        >
          {renderItem(info.item)}
        </Animated.View>
      )}
    />
  )
}
2reactions
Swiftworkcommented, Jan 2, 2022

I also notice that using:

Animated.createAnimatedComponent(FlatList)

or:

Animated.FlatList

breaks type inference of the children. For the first example:

Animated.createAnimatedComponent<FlatListProps<ChildType>>(FlatList)

can be used to restore the types. This should probably be handled more generically with Animated.FlatList. The definition for Animated.FlatList should probably be something along the lines of:

export class FlatList<T = any> extends Component<AnimateProps<FlatListProps<T>>> {
  itemLayoutAnimation: ILayoutAnimationBuilder;
  getNode(): ReactNativeFlatList;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Animation lags with inverted Flatlist (Expo / TypeScript)
I was trying to implement a WhatsApp like cancelling recording button. Everything was working fine, all animations lauching correctly, ...
Read more >
FlatList - React Native
FlatList. A performant interface for rendering basic, flat lists, supporting the most handy features: Fully cross-platform.
Read more >
[FlatList] Scrolling goes wrong with inverted prop - Gitcoin
[FlatList] Scrolling goes wrong with inverted prop. TypeScript. JavaScript. React Native. react-native-modalize. FlatList. 0.25 ETH. 0.00 USD. cancelled.
Read more >
react-native-inverted-flat-list - npm
React native inverted flat list with the ability to use pull to refresh. ... your project by running `npm i react-native-inverted-flat-list`.
Read more >
MessageList | Stream Chat - React Native SDK Docs
Since we use inverted FlatList, this component will be rendered at top of the list. In absence of this prop, an inline loading...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found