Fix a few instances of react/no-unstable-nested-components

This commit is contained in:
Brandon Presley 2023-07-17 18:38:28 +12:00
parent 040d588b5a
commit 36d3de401b
4 changed files with 101 additions and 85 deletions

View File

@ -1,6 +1,6 @@
import React, { useCallback, useMemo, useState } from 'react' import React, { useCallback, useMemo, useState } from 'react'
import { View } from 'react-native' import { View } from 'react-native'
import { Button, Menu, Subheading, useTheme } from 'react-native-paper' import { Button, Menu, Subheading } from 'react-native-paper'
import { ITEM_PADDING } from './constants' import { ITEM_PADDING } from './constants'
export interface Item { export interface Item {
@ -21,7 +21,6 @@ function Select({
label?: string label?: string
}) { }) {
const [show, setShow] = useState(false) const [show, setShow] = useState(false)
const { colors } = useTheme()
const selected = useMemo( const selected = useMemo(
() => items.find((item) => item.value === value) || items[0], () => items.find((item) => item.value === value) || items[0],

View File

@ -42,24 +42,19 @@ export default function SetItem({
return LIGHT_RIPPLE return LIGHT_RIPPLE
}, [dark, ids, item.id]) }, [dark, ids, item.id])
const left = useCallback(() => {
if (!settings.images || !item.image) return null
return ( return (
<List.Item
onPress={press}
title={item.name}
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
onLongPress={longPress}
style={{ backgroundColor }}
left={() =>
settings.images &&
item.image && (
<Image <Image
source={{ uri: item.image }} source={{ uri: item.image }}
style={{ height: 75, width: 75 }} style={{ height: 75, width: 75 }}
/> />
)} )
right={() => ( }, [item.image, settings.images])
<>
{settings.showDate && ( const right = useCallback(() => {
if (!settings.showDate) return null
return (
<Text <Text
style={{ style={{
alignSelf: 'center', alignSelf: 'center',
@ -68,9 +63,18 @@ export default function SetItem({
> >
{format(new Date(item.created), settings.date || 'P')} {format(new Date(item.created), settings.date || 'P')}
</Text> </Text>
)} )
</> }, [settings.showDate, item.created, settings.date, dark])
)}
return (
<List.Item
onPress={press}
title={item.name}
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
onLongPress={longPress}
style={{ backgroundColor }}
left={left}
right={right}
/> />
) )
} }

View File

@ -46,7 +46,7 @@ export default function StartPlanItem(props: Props) {
[setShowMenu, setAnchor], [setShowMenu, setAnchor],
) )
const edit = async () => { const edit = useCallback(async () => {
const now = await getNow() const now = await getNow()
const created = now.split('T')[0] const created = now.split('T')[0]
const first = await setRepo.findOne({ const first = await setRepo.findOne({
@ -60,28 +60,23 @@ export default function StartPlanItem(props: Props) {
setShowMenu(false) setShowMenu(false)
if (!first) return toast('Nothing to edit.') if (!first) return toast('Nothing to edit.')
navigate('EditSet', { set: first }) navigate('EditSet', { set: first })
} }, [item.name, navigate])
return ( const left = useCallback(
<List.Item () => (
onLongPress={longPress}
title={item.name}
description={item.sets
? `${item.total} / ${item.sets}`
: item.total.toString()}
onPress={() => onSelect(index)}
left={() => (
<View style={{ alignItems: 'center', justifyContent: 'center' }}> <View style={{ alignItems: 'center', justifyContent: 'center' }}>
<RadioButton <RadioButton
onPress={() => onPress={() => onSelect(index)}
onSelect(index)}
value={index.toString()} value={index.toString()}
status={selected === index ? 'checked' : 'unchecked'} status={selected === index ? 'checked' : 'unchecked'}
color={colors.primary} color={colors.primary}
/> />
</View> </View>
)} ),
right={() => ( [index, selected, colors.primary, onSelect],
)
const right = useCallback(() => (
<View <View
style={{ style={{
width: '25%', width: '25%',
@ -97,7 +92,18 @@ export default function StartPlanItem(props: Props) {
<Menu.Item leadingIcon='undo' onPress={undo} title='Undo' /> <Menu.Item leadingIcon='undo' onPress={undo} title='Undo' />
</Menu> </Menu>
</View> </View>
)} ), [anchor, showMenu, edit, undo])
return (
<List.Item
onLongPress={longPress}
title={item.name}
description={item.sets
? `${item.total} / ${item.sets}`
: item.total.toString()}
onPress={() => onSelect(index)}
left={left}
right={right}
/> />
) )
} }

View File

@ -40,22 +40,18 @@ export default function WorkoutItem({
return `${item.sets} x ${item.minutes || 0}:${seconds}` return `${item.sets} x ${item.minutes || 0}:${seconds}`
}, [item]) }, [item])
const left = useCallback(() => {
if (!images || !item.image) return null
return ( return (
<>
<List.Item
onPress={() => navigation.navigate('EditWorkout', { value: item })}
title={item.name}
description={description}
onLongPress={longPress}
left={() =>
images &&
item.image && (
<Image <Image
source={{ uri: item.image }} source={{ uri: item.image }}
style={{ height: 75, width: 75 }} style={{ height: 75, width: 75 }}
/> />
)} )
right={() => ( }, [item.image, images])
const right = useCallback(() => {
return (
<Text <Text
style={{ style={{
alignSelf: 'center', alignSelf: 'center',
@ -76,7 +72,18 @@ export default function WorkoutItem({
/> />
</Menu> </Menu>
</Text> </Text>
)} )
}, [anchor, showMenu, item.name])
return (
<>
<List.Item
onPress={() => navigation.navigate('EditWorkout', { value: item })}
title={item.name}
description={description}
onLongPress={longPress}
left={left}
right={right}
/> />
<ConfirmDialog <ConfirmDialog
title={`Delete ${showRemove}`} title={`Delete ${showRemove}`}