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.

ForwardRef and Animated.FlatList not set

See original GitHub issue

Description

Should Animated.FlatList be using forwardRef like this?

const ReanimatedFlatlist = forwardRef<FlatList, ReanimatedFlatlistProps<any>>(
  ({ itemLayoutAnimation, ...restProps }, ref) => {
    const cellRenderer = React.useMemo(() => createCellRenderer(itemLayoutAnimation), []);
    return <AnimatedFlatList ref={ref} {...restProps} CellRendererComponent={cellRenderer} />;
  },
);

I noticed i couldn’t use useRef with Animated.FlatList as you get the warning about Function components cannot be given refs

I’m happy to raise a PR, do let me know

Expected behavior

can use useRef with Animated.FlatList, particular to scroll with react navigation useScrollToTop hook

Actual behavior & steps to reproduce

Couldn’t use useRef with Animated.FlatList as you get the warning about Function components cannot be given refs

Package versions

name version
react-native-reanimated 2.3
expo 44

Affected platforms

  • Android
  • iOS
  • Web

ps Thank you for an amazing library

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:14
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
axelnormandcommented, Mar 17, 2022

Sure thing

import React, { forwardRef } from 'react';
import { FlatList, FlatListProps, LayoutChangeEvent, View } from 'react-native';
import Animated, { ILayoutAnimationBuilder } from 'react-native-reanimated';

const ReanimatedFlatList = Animated.createAnimatedComponent(FlatList);
const AnimatedView = Animated.createAnimatedComponent(View);

const createCellRenderer = (itemLayoutAnimation?: ILayoutAnimationBuilder) => {
  const cellRenderer: React.FC<{
    onLayout: (event: LayoutChangeEvent) => void;
  }> = (props) => {
    return (
      <AnimatedView layout={itemLayoutAnimation as never} onLayout={props.onLayout}>
        {props.children}
      </AnimatedView>
    );
  };

  return cellRenderer;
};

interface ReanimatedFlatlistProps<T> extends FlatListProps<T> {
  itemLayoutAnimation?: ILayoutAnimationBuilder;
}

/**
 * re-create Reanimated FlatList but correctly pass on forwardRef so can use useScrollToTop in react navigation
 *
 * Source: https://github.com/software-mansion/react-native-reanimated/blob/main/src/reanimated2/component/FlatList.tsx
 *
 * TODO: remove this and use Animated.FlatList directly when can use refs with it. Also type the generic T properly for FlatList and dont use `any`
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const AnimatedFlatList = forwardRef<FlatList, ReanimatedFlatlistProps<any>>(
  ({ itemLayoutAnimation, ...restProps }, ref) => {
    // eslint-disable-next-line react-hooks/exhaustive-deps
    const cellRenderer = React.useMemo(() => createCellRenderer(itemLayoutAnimation), []);
    return <ReanimatedFlatList ref={ref} {...restProps} CellRendererComponent={cellRenderer} />;
  },
);
3reactions
gurupatel107commented, Jul 28, 2022

Can we have this available as a patch version in next 2.9.2 version ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript forwardRef error in react native - Stack Overflow
To be honest, it feels weird to see the forwardRef in this context. I mean, what you really want is to have the...
Read more >
Direct Manipulation - React Native
If you run this you will immediately see this error: Touchable child must either be native or forward setNativeProps to a native component...
Read more >
react-native-super-grid - npm
Methods called on SectionList/FlatList refs can be called directly now (because of forwardRef). i.e Instead of ref.current.sectionList.
Read more >
property 'ref' does not exist on type 'intrinsicattributes - You.com
So the main problem here is that you're returning the result of React.forwardRef in your render, which isn't a valid return type for...
Read more >
Why do I get error when I use forwardRef in react-native?
const Audio = React.forwardRef((props,ref) => { return( // error is here component name must starts with a capital letter <audio src={Song} ...
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