below is my code:
const ProductData = () => {
const [data, setData] = useState([])
const x = [{"product": {"product_description": "XYZ", "product_name": "Product1"}, "quantity": 1}]
setData(x);
function onChangeQuantity(i, type){
let quant = data[i].quantity;
if (type) {
quant += 1;
data[i].quantity = quant;
setData(data);
console.log(data);
} else if (type === false && quant >= 2) {
quant -= 1;
data[i].quantity = quant;
setData(data);
console.log(data);
} else if (type === false && quant === 1) {
data.splice(i, 1);
setData(data);
}
}
return(
<View>
{data.map((item, index) => (
<View key={index}>
<Text>
{item.product.product_name}
</Text>
</View>
<View style={styles.container4}>
<TouchableOpacity
onPress={() => onChangeQuantity(index, false)}>
<Text> Decrement Value </Text>
</TouchableOpacity>
<Text>{item.quantity}</Text>
<TouchableOpacity
onPress={() => onChangeQuantity(index, true)}>
<Text> Increment Value </Text>
</TouchableOpacity>
</View>
</View>
))}
</View>
)
}
Now the issue am facing is - whenever I hit the Increment Value/ Decrement value the quantity is getting updated but the updated value is not getting displayed inside the return statement. Sample console log after Increment Value button is pressed is as follows:
[{"product": {"product_description": "Product2", "product_name": "Product2"}, "quantity": 2}]
But the change is not reflected in the View. How do I display the updated quantity immediately after the button is clicked. Please let me know where am I going wrong. Thanks a lot in advance.