The problem is when I update the quantity of an item the parent component start to re-render again I want to understand what am doing wrong I already using memo to stop doing the re-render so what should i do if i wanna fix the re-render and only update the object on array with out re-rendring, if somebody can help me with that? Thanks in advance
Parent Component
export const ProductsScreen = ({ }) => {
const [Products, setProducts] = useState([
// sample of object props
{
id: '1',
name: 'first item',
volume: 500,
price: 150.25,
total: 4,
qty: 0
}]
const renderProducts = ({ item }) => {
const { qty } = item
return (
<ProductListItems
product={item}
qty={qty}
onAddItem={onAddItem}
onRemoveItem={onRemoveItem} />
)
}
const updateObjectInArray = (newProduct) => {
return setProducts(
Products.map((item) => {
if (item.id !== newProduct.id) {
return item
}
return {
...item,
...newProduct
}
})
)
}
const onAddItem = productId => {
let product = Products.find(i => {
return i.id === productId
})
const { total, qty } = product
const newProduct = {
...product,
qty: qty + 1,
total: total - 1
}
updateObjectInArray(newProduct)
}
const onRemoveItem = productId => {
let product = Products.find(i => {
return i.id === productId
})
const { total, qty } = product
const newProduct = {
...product,
qty: qty - 1,
total: total + 1
}
updateObjectInArray(newProduct)
}
return (
console.log("RERENDERD"),
<View style={styles.container} >
<SafeAreaView style={{ flex: 1, }}>
<FlatList
contentContainerStyle={{ paddingVertical: 15 }}
showsVerticalScrollIndicator={false}
data={Products}
renderItem={renderProducts}
keyExtractor={item => item.id}
/>
</SafeAreaView>
</View >
)
};
Child Component
const ProductListItems = ({
product,
qty,
onAddItem,
onRemoveItem
}) => {
return (
// design here
)
};
export default memo(ProductListItems, (prevState, nextState) => prevState.qty === nextState.qty);