score:1

import React, { useContext } from "react";
import { CartContext } from "./CartContext";

function Cart() {
  const [cart, setCart] = useContext(CartContext);

  function updateCart() {
    setCart(prevState => {
      const newState = Array.from(prevState);
      newState.push("c");
      return newState;
    });
  }

  return (
    <React.Fragment>
      <div>I am Cart</div>
      <div>Cart value: {JSON.stringify(cart)}</div>
      <button onClick={updateCart}>Add 'c' to cart</button>
    </React.Fragment>
  );
}

export default Cart;

Related Query

More Query from same tag