From c1c9338ab09c287451ac426c4a52e3ed95ba4e0d Mon Sep 17 00:00:00 2001 From: Anish Sarawgi <98693953+Anish-Sarawgi@users.noreply.github.com> Date: Wed, 19 Jul 2023 18:25:37 +0530 Subject: [PATCH] fix(photostory): optimise the state update in photostory (#444) * fix(photostory): optimise the state update * fix(photostory): update right side image in carousel --- client/src/components/photostory/Carousel.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/client/src/components/photostory/Carousel.js b/client/src/components/photostory/Carousel.js index dd6c89d3..ad8e7d58 100644 --- a/client/src/components/photostory/Carousel.js +++ b/client/src/components/photostory/Carousel.js @@ -13,19 +13,15 @@ const Carousel = ({ content }) => { const classes = useStyles(); const length = content.length; - const [left, setLeft] = useState(length - 1); const [current, setCurrent] = useState(0); - const [right, setRight] = useState(1); + const left = current === 0 ? length - 1 : current - 1; + const right = current === length - 1 ? 0 : current + 1; const nextSlide = () => { - setLeft(left === length - 1 ? 0 : left + 1); setCurrent(current === length - 1 ? 0 : current + 1); - setRight(right === length - 1 ? 0 : right + 1); }; const prevSlide = () => { - setLeft(left === 0 ? length - 1 : left - 1); setCurrent(current === 0 ? length - 1 : current - 1); - setRight(right === 0 ? length - 1 : right - 1); }; if (!Array.isArray(content) || content.length <= 0) {