Open AlbertMontolio opened 4 years ago
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.
startSlide: position
Hope that helps.
I have this problem to ^^ I try startSlide and it works but we lost the animation.
i get animations if i use transitionEnd instead of callback:
swipeOptions={{
continuous: false,
transitionEnd: (p) => setPage(p),
startSlide: page,
}}
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. You just need to wrap swipeOptions in useMemo.
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: