first i make price int array and store int numbers in hook only once then i want to update specific index of that price array in update value but didnt able to update setPrice specific index instead it return me to array with only one value but i want all values with updated index value like [0,3,4,5,6] update id 1 then in my logic multiply by 2 it should do [0,6,4,5,6] but it return me only [6]
import React, {useEffect,useState,useContext } from 'react';
import {Cartlist} from '../Global/CartContext.js';
const Cart=()=>{
const {ShoppingCart,totalprice,quantity,dispatch} = useContext(Cartlist);
const [price,setPrice]=useState([])
const Style={
width:'100px',
height: '100px',
}
useEffect(()=>{
ShoppingCart.map((products)=>{
setPrice(prev=>[...prev,products.price]);
})
},[])
const updatevalue=(e)=>{
ShoppingCart.map((products)=>{
if (e.target.id===products.id){
setPrice(price=>price[products.id-1]*2);
}
})
}
return (
<div className='container'>
{ShoppingCart.length > 0 ?
ShoppingCart.map((products)=>(
<div>
<span><img style={Style} src={products.image}></img></span>
<span>{price}</span>
<span>{quantity}</span>
<span><button id={products.id} onClick={updatevalue}>+</button></span>
</div>
))
:''}
</div>
)
}
export default Cart