I can't figure out what's going on. I add an object to the array, it is added, but the old data copies the values from the new object. This is the first time I've encountered this.

Here is the code

      var next
      if(cart === null){
        next = 0
      }else {
        next = cart.length
      }
      switch(type){
        case 'horizontal':
          config['0']['type'] = type
          config['0']['id'] = next
          config['0'].idImg = selectSvg
          if(cart === null || cart === undefined || cart.length < 1){
            const news = []
            news.push(config)
            setCountCart(news.length)
            localStorage.setItem('cart-tagstyle', JSON.stringify(news))
            setCart(news)
          }else {
            const news = [...cart]
            console.log(news);
            news.push(config)
            console.log(news);
            setCountCart(news.length)
            localStorage.setItem('cart-tagstyle', JSON.stringify(news))
            setCart(news)
          }
          break;

enter image description here enter image description here enter image description here enter image description here

I tried it without push, according to the following index, the same thing. I searched the Internet, it didn't help. I've been suffering for the second day

0

There are 0 best solutions below