jemise111 / react-native-swipe-list-view

A React Native ListView component with rows that swipe open and closed
https://www.npmjs.com/package/react-native-swipe-list-view
MIT License
2.79k stars 527 forks source link

React 16.13.1 - Warning: function components cannot be given refs. Use forwardRef #527

Closed florinvasilevilsan closed 3 years ago

florinvasilevilsan commented 4 years ago

Your issue may already be reported! Please search on the issue tracker before creating one.

Describe the bug On React 16.13.1 getting 'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()'.

This is due to React changes where we have been asked to rename ref to 'forwardRef'

To Reproduce Use library with the indicated react version

Environment (please complete the following information):

jspizziri commented 3 years ago

@florinvasilevilsan , not sure how you've implemented this, but I'm guessing that you're returning a functional component in your renderItem callback. If that's the case you need to make sure you wrap the component returned from renderItem in a forwardRef like below:

const List = () => {
  return (
    <SwipeListView
      renderItem={({ index, item }, rowMap) => (
        <Foo key={index} item={item} />
      )}
    />
  );
}

const Foo = React.forwardRef(({ item }, ref) => {
  return (
    <Text>{item.name}</Text>
  );
});
florinvasilevilsan commented 3 years ago

@florinvasilevilsan , not sure how you've implemented this, but I'm guessing that you're returning a functional component in your renderItem callback. If that's the case you need to make sure you wrap the component returned from renderItem in a forwardRef like below:

const List = () => {
  return (
    <SwipeListView
      renderItem={({ index, item }, rowMap) => (
        <Foo key={index} item={item} />
      )}
    />
  );
}

const Foo = React.forwardRef(({ item }, ref) => {
  return (
    <Text>{item.name}</Text>
  );
});

Thanks, that sorted it for me.

roy-rayyone commented 3 years ago

Just wrap your <Foo key={index} item={item} /> in a View tag is good enough