score:0

const [quantity, setQuantity] = useState(1);
const cartItems = useSelector((state) => state.cart.cartItems);
const dispatch = useDispatch();
useSaveToLocalStorage(state.cart.cartItems, { key: 'cart.items',
                                                  value: state.cart.cartItems });

const handleAddToCart = (pizza, varient, quantity) => {
  let item = {
    id: pizza._id,
    name: pizza.name,
    image: pizza.image,
    varient,
    quantity,
    price: pizza.prices[0][varient] * quantity,
    prices: pizza.prices,
  };
  dispatch(addItemToCart(item));
  dispatch(setTotalPrice());
  dispatch(setCartCount());

  // i want to grab the current state here after it has been updated in line 27 and save it in local storage
};

score:0

useEffect(() => {
    if (cartItems) {
      // Do your thing here
    }
}, [cartItems]); // Guaranteed to be triggered *after* each update

Related Query

More Query from same tag