From 4ec67ab5fe9de5ff940a3bf0a0c65148fe783ce6 Mon Sep 17 00:00:00 2001 From: caxewsh Date: Fri, 8 Nov 2024 18:57:36 +0100 Subject: [PATCH] fix(gameMode): memoize carousel items to avoid re-render --- components/modescreen/GameModeCarousel.jsx | 52 ++++++++++++---------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/components/modescreen/GameModeCarousel.jsx b/components/modescreen/GameModeCarousel.jsx index 495a382..2109f1d 100644 --- a/components/modescreen/GameModeCarousel.jsx +++ b/components/modescreen/GameModeCarousel.jsx @@ -3,6 +3,33 @@ import { View, Text, TouchableOpacity, Image } from "react-native"; import Carousel from "react-native-snap-carousel"; import { useNavigation } from "@react-navigation/native"; +// Memoized functional component for each item +const CarouselItem = React.memo(({ item, navigation }) => ( + + + + + {item.title} + + item.available && navigation.navigate("Lobby")} + className={`py-2 px-4 rounded-full ${ + item.available ? 'bg-cyan-700' : 'bg-gray-400' + }`} + disabled={!item.available} + > + + {item.available ? 'Choisir' : 'Indisponible'} + + + + +)); + const GameModeCarousel = () => { const navigation = useNavigation(); @@ -28,29 +55,7 @@ const GameModeCarousel = () => { ]; const renderItem = ({ item }) => ( - - - - - {item.title} - - item.available && navigation.navigate("Lobby")} - className={`py-2 px-4 rounded-full ${ - item.available ? 'bg-cyan-700' : 'bg-gray-400' - }`} - disabled={!item.available} - > - - {item.available ? 'Choisir' : 'Indisponible'} - - - - + ); return ( @@ -63,6 +68,7 @@ const GameModeCarousel = () => { loop autoplay autoplayInterval={5000} + initialNumToRender={3} // Adjust for better performance /> );