notrab / react-use-cart

React hook library for managing cart state
http://npm.im/react-use-cart
Apache License 2.0
367 stars 48 forks source link

Setters methods not found #78

Closed darvinsilveira closed 3 years ago

darvinsilveira commented 3 years ago

Hi, thanks for the great work.

I'm trying to use methods like addItem, removeItem...and I keep getting addItem is not a function. I followed all the examples and properties like isEmpty or cartTotal are working just fine, but when trying to use a method I get an error. I'm using NextJS. Here a simple example:

`import { CartProvider, useCart } from "react-use-cart";

const ProductDetails = () => {

const { addItem } = useCart();

const product = {
    id: 1,
    name: "Sports Hooded Sweatshirt",
    price: 18.99
}

useEffect(() => {
    addItem(product);
}, []);

`

The error:

image

I've added the addItem inside useEffect for testing purposes.

My page is wrapped by CartProvider also, but I think this is just to keep track of the states.

I don't know if is a bug or what is, so I decide to report this as an issue. Sorry if it's not the case.

notrab commented 3 years ago

Hey

I'm not too sure about the exact error you're referring to since it works in a fresh install I did just now - as you can see here

Can you share more of your code?

darvinsilveira commented 3 years ago

Hi Jamie, you can close the issue, I found out the problem.

I thought I could call CartProvider component only on the parts where I show my cart, but it HAVE to be around the entire application.

Sorry for bothering you with this and thank you so much for the fast reply.