title: I want to fix a "TypeError: Converting circular structure to JSON" error in
body: <p>I want to an "add to cart" feature on a personal project(using Javascript) by saving each food info to localstorage upon clicking but getting this "TypeError: Converting circular structure to JSON" error</p>
  
  <p>I have tried the fast-safe-stringify and circular-json packages but to no avail</p>
  
  <pre><code>export const addFood = (food, next) =&gt; {
    let cart = [];
    if (typeof window !== "undefined") {
      if (localStorage.getItem("cart")) {
        cart = JSON.parse(localStorage.getItem("cart"));
      }
      cart.push({
        ...food,
        count: 1
      });
      cart = Array.from(new Set(cart.map(f =&gt; f._id))).map(id =&gt; {
        return cart.find(f =&gt; f._id === id);
      });
      const cartToString = JSON.stringify(cart);
      localStorage.setItem("cart", cartToString);
      next();
    }
  };
  
  TypeError: Converting circular structure to JSON
      --&gt; starting at object with constructor 'FiberNode'
      |     property 'stateNode' -&gt; object with constructor 'HTMLButtonElement'
      --- property '__reactInternalInstance$v1zqxvbiiip' closes the circle
  addFood
  src/components/layouts/cartHelpers.js:16
    13 |   cart = Array.from(new Set(cart.map(f =&gt; f._id))).map(id =&gt; {
    14 |     return cart.find(f =&gt; f._id === id);
    15 |   });
  &gt; 16 |   const cartToString = JSON.stringify(cart);
       | ^  17 |   localStorage.setItem("cart", cartToString);
    18 |   next();
    19 | }
  </code></pre>