'setCookies doesn't update cookies in react
I have a problem with setCookies of useCookie library, when i try to save a state to cookies there are no changes, it is very strange because in other parts of the program it works without problems
This is my code:
const [cart,setCart] = useState(null)
const [cookies, setCookies , removeCookies] = useCookies(['cart']);
useEffect(()=>{
setCart(cookies['cart'])
},[])
const submitOrder = () =>{
switch (type) {
case 0:
if (!cart.home_delivery){
setCart(prevState => ({
...prevState,
['home_delivery']:[ordine]
}
))
}else{
setCart(prevState => ({
...prevState,
['home_delivery']:[...prevState.home_delivery,ordine]
}
))
}
console.log(cart) //new cart output is right
setCookies('cart',cart,{path:'/'}) //this doesn't update cookies
case 1:
return 'take_away'
case 2:
return 'reservation'
case 3:
return 'shipping'
}
}
useEffect(()=>{
if(cart){
console.log(cart) //here cart is always right
setCookies('cart',cart,{path:'/'}) //this doesn't update or changes cookies
setCookies('cart','some string',{path:'/'}) //this work
}
},[cart])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
