react-native-swipeable icon indicating copy to clipboard operation
react-native-swipeable copied to clipboard

Recenter item in functional component

Open plaporte opened this issue 6 years ago • 4 comments

I'm using Swipeable component within a functional component. How can I call recenter() correctly?

plaporte avatar Jan 21 '20 22:01 plaporte

HI, I have the same problem. Is there any solution?

AramArakelyan22 avatar Apr 21 '20 10:04 AramArakelyan22

@plaporte @AramArakelyan22 You can try something like this:

const SWIPE_LENGTH = 55;

export const SwipeableComponent = React.memo(({ id, onRemove }) => {
  let [swipeRef] = useState(null);

  const onRemove = useCallback(() => onRemove(id), [id]);

  const renderRemoveButton = useCallback(() => (
    <TouchableOpacity
      onPress={() => {
        onDeleteClick();
        swipeRef.recenter();
      }}>
    </TouchableOpacity>
  ), []);

  return (
    <Swipeable
      onRef={(ref) => { swipeRef = ref; }}
      rightButtons={[renderRemoveButton()]}
      rightButtonWidth={SWIPE_LENGTH}/>
  );
});

All you need is useState, then you need to center it in some kind of action (press button and etc). And dont forget about ref in onRef.

If you have any suggestions for improvement, go on :)

bataevvlad avatar Apr 22 '20 11:04 bataevvlad

i tried this but can't seem to get it working with a functional component

nescroft avatar Dec 20 '20 05:12 nescroft

I was able to get it working using ref instead of onRef as well as close() instead of recenter()


  const swipeRef = useRef();

  const doRecenter = function(){
    if(swipeRef && swipeRef.current){
      swipeRef.current.close();
    }
  }

  return(
    <Swipeable
        ref={(ref) => {swipeRef.current = ref}}
    />
  )

scottysmalls avatar Aug 16 '22 01:08 scottysmalls