Wrap SetItem in React.memo

Apparently large lists in React Native should be wrapped in this,
as long as they don't have any internal state.

So this should theoretically improve performance when you scroll down
a lot.
This commit is contained in:
Brandon Presley 2023-11-15 11:29:40 +13:00
parent ba24649a52
commit ec17ad5805
1 changed files with 71 additions and 66 deletions

View File

@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from "@react-navigation/native";
import { format } from "date-fns";
import { useCallback, useMemo } from "react";
import React, { useCallback, useMemo } from "react";
import { Image } from "react-native";
import { List, Text, useTheme } from "react-native-paper";
import { StackParams } from "./AppStack";
@ -8,21 +8,22 @@ import { DARK_RIPPLE, LIGHT_RIPPLE } from "./constants";
import GymSet from "./gym-set";
import Settings from "./settings";
export default function SetItem({
const SetItem = React.memo(
({
item,
settings,
ids,
setIds,
disablePress,
customBg,
}: {
}: {
item: GymSet;
settings: Settings;
ids: number[];
setIds: (value: number[]) => void;
disablePress?: boolean;
customBg?: string;
}) {
}) => {
const { dark } = useTheme();
const navigation = useNavigation<NavigationProp<StackParams>>();
@ -33,7 +34,8 @@ export default function SetItem({
const press = useCallback(() => {
if (disablePress) return;
if (ids.length === 0) return navigation.navigate("EditSet", { set: item });
if (ids.length === 0)
return navigation.navigate("EditSet", { set: item });
const removing = ids.find((id) => id === item.id);
if (removing) setIds(ids.filter((id) => id !== item.id));
else setIds([...ids, item.id]);
@ -78,4 +80,7 @@ export default function SetItem({
)}
/>
);
}
}
);
export default SetItem;