-
Notifications
You must be signed in to change notification settings - Fork 256
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
How to use this library with useState hook? #165
Comments
I believe the component will re-render as you are updating state, but you just need to set the startSlide option to the current position in the swipeOptions so that it renders with the correct starting slide.
Hope that helps. |
I have this problem to ^^ |
i get animations if i use transitionEnd instead of callback:
|
When I proceeded in this way, there was an issue where ReactSwipe was re-rendered every time I swipe. The following is a method using useMemo found on StackOverflow. const swiper = useRef(null);
const swipeOptions = useMemo(() => ({
startSlide: 0,
continuous: false,
}), []);
return (
<ReactSwipe swipeOptions={swipeOptions} ref={swiper} >
<StyledBox>PANE 1</StyledBox>
<StyledBox>PANE 2</StyledBox>
<StyledBox>PANE 3</StyledBox>
</ReactSwipe>
); or const swiper = useRef(null);
const [position, setPosition] = useState(0)
const swipeOptions = useMemo(() => ({
startSlide: position,
continuous: false,
transitionEnd: (index) => setPosition(index),
}));
return (
<ReactSwipe swipeOptions={swipeOptions} ref={swiper} >
<StyledBox>PANE 1</StyledBox>
<StyledBox>PANE 2</StyledBox>
<StyledBox>PANE 3</StyledBox>
</ReactSwipe>
); |
Description
I want to track the position of the slide in a state variable, so that I can use it in another component. The problem is, whenever I use setValue of the useState hook, all the page gets re-rendered.
Here is the code:
The text was updated successfully, but these errors were encountered: