Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recenter item in functional component #92

Open
plaporte opened this issue Jan 21, 2020 · 4 comments
Open

Recenter item in functional component #92

plaporte opened this issue Jan 21, 2020 · 4 comments

Comments

@plaporte
Copy link

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

@AramArakelyan22
Copy link

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

@bataevvlad
Copy link

bataevvlad commented Apr 22, 2020

@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 :)

@nescroft
Copy link

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

@scottysmalls
Copy link

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}}
    />
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants