I am newbie to react and i am trying to learn useReducer and useContext. What i am trying to do is add the product to the cart and adding it to the context variable by using setContext,
const [context, setContext] = useContext(Context)
const [cart, dispatch] = useReducer(reducer, [])
but the setContext(cart) doesnt updates the context on the first time when i click "ADD TO CART" button. The context variable returns an empty array, but the cart is updated and renders with the product which i added. But during the next time when i click "ADD TO CART" it updates the context variable. I am not able to find the reason why it is. Could anyone help me understand why and how to rectify it. Here is the codesandbox which i am using to work and study this. Please make changes if you find anything wrong. Codesandbox