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.

Rethinking the "Compat" abstraction (types/reanimated issue)

See original GitHub issue

The RecyclerListView component is rather exotic in the react ecosystem, since it does not directly derive from/extend ComponentType<P, S, SS>. Instead, it derives from/extends ComponentCompat<T1, T2, SS>, which itself extends from React.Component<T1, T2, SS>. While this works fine at runtime, it doesn’t work when trying to create HoCs or “wrappers” for the RecyclerListView component.

An example for this would be the createAnimatedComponent function from react-native-reanimated, which enables all props and style-props to be animated values/shared values. To maintain type-safety for this function, it is expected to receive a parameter of type ComponentType<P>, which doesn’t match the RecyclerListView’s type causing the following TypeScript errors:

src/screens/main/home/HomeScreen.tsx:45:71 - error TS2345: Argument of type 'typeof RecyclerListView' is not assignable to parameter of type 'ComponentType<{ style?: StyleProp<object>; }>'.
  Type 'typeof RecyclerListView' is not assignable to type 'ComponentClass<{ style?: StyleProp<object>; }, any>'.
    Types of property 'defaultProps' are incompatible.
      Type '{ canChangeSize: boolean; disableRecycling: boolean; initialOffset: number; initialRenderIndex: number; isHorizontal: boolean; onEndReachedThreshold: number; renderAheadOffset: number; }' has no properties in common with type 'Partial<{ style?: StyleProp<object>; }>'.

45 const ReanimatedRecyclerListView = Reanimated.createAnimatedComponent(RecyclerListView);
                                                                         ~~~~~~~~~~~~~~~~

src/screens/main/home/HomeScreen.tsx:408:11 - error TS2322: Type '{ style: { marginTop: number; flex: number; }; rowRenderer: (type: unknown, data: Post | "ad") => Element | null; dataProvider: DataProvider; layoutProvider: LayoutProvider; ... 5 more ...; scrollEventThrottle: number; }' is not assignable to type 'IntrinsicAttributes & AnimateProps<object, { style?: StyleProp<object>; }> & { children?: ReactNode; }'.
  Property 'rowRenderer' does not exist on type 'IntrinsicAttributes & AnimateProps<object, { style?: StyleProp<object>; }> & { children?: ReactNode; }'.

408           rowRenderer={rowRenderer}
              ~~~~~~~~~~~

In other words: Animated.createAnimatedComponent(RecyclerListView) does not work with TypeScript, so we can’t run animations using the onScroll event. (meaning we have “compile”-time errors and no type safety.)

This issue should be a discussion point if we can do something about this in the RecyclerListView types, or if this has to be ignored and casted to unknown, causing all type-safety to be lost. Do we even need the ComponentCompat.ts anymore? Looks like it’s not doing anything specific…

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
likerncommented, Apr 19, 2021

I actually have the same question ^

Does anyone have experience using reanimated with recycler list view? Do you use Animated.createAnimatedComponent and pass an onScroll prop, or do you pass a renderScrollComponent prop with an Animated.ScrollView?

I can circumvent the types issues with ts-expect-error if needed.

@nandorojo I’ve experimented with it for react-native-calendar and had working prototype. Unfortunately, that code was not saved somewhere.

I had not seen any performance benefits and, probably, it was even worse than FlatList. At least didn’t solve performance issues. So I switched back to just using FlatList, trying to squeeze maximum out of it.

As far as I remember I couldn’t not make it working purely on UI. Probably I used renderScrollComponent to provide Animated.ScrollView. Then I used onScroll on that Animated.ScrollView to calculate / run animations on UI / etc.

And you have to report scroll changes back to RecyclerListView, otherwise it wouldn’t know about changes happening. So finally I had to call runOnJS(). Probably it ruined the overall idea and performance. Or, at least, I didn’t get what expected.

0reactions
likerncommented, Sep 12, 2021

@nandorojo Hello ✋🏻 Do you still need interpolating list item style based on the scroll position?

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-native-reanimated - npm
React Native's Animated library reimplemented. It provides a more comprehensive, low level abstraction for the Animated library API to be built ...
Read more >
About React Native Reanimated - Software Mansion
Reanimated provides a more comprehensive, low level abstraction for the Animated library API, giving you much greater flexibility, control and performance.
Read more >
RETHINKING NEOLIBERALISM
This book is a collection of essays from a two-year-long faculty seminar on neoliberalism at Hunter College, CUNY. Various presenters contributed chapters,.
Read more >
kmagiera/react-native-reanimated - GitHub - JOYK
React Native's Animated library reimplemented. It provides a more comprahensive, low level abstraction for the Animated library API to be build on top...
Read more >
Full text of "Culture And Imperialism By Edward W. Said"
Culture and the aesthetic forms it contains derive from historical ... For it to become a site where social and political issues are...
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