Created
August 27, 2019 21:03
-
-
Save dayhaysoos/796eb7719424a499bd40f7ca93f54183 to your computer and use it in GitHub Desktop.
context stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx jsx */ | |
import React, { Component } from 'react'; | |
import { Styled, jsx } from 'theme-ui' | |
import { useSkus, CartProvider } from '../context/shopping-cart'; | |
const Sku = (props) => { | |
console.log(props) | |
const redirectToCheckout = async (event) => { | |
event.preventDefault(); | |
const { skuID } = props; | |
const { error } = await this.stripe.redirectToCheckout({ | |
items: [{ sku: skuID, quantity: 1 }], | |
successUrl: `http://localhost:8000/`, | |
cancelUrl: `http://localhost:8000/`, | |
}) | |
if (error) { | |
console.warn("Error:", error) | |
} | |
} | |
const { name, price, image, skuID } = props; | |
const { addItem } = useSkus(); | |
return ( | |
<div> | |
<Styled.img src={image} /> | |
<Styled.p>{name}</Styled.p> | |
<Styled.p>$ {price}</Styled.p> | |
<button onClick={redirectToCheckout}>Purchase item</button> | |
<button onClick={() => addItem(skuID)}>Add to Cart</button> | |
</div> | |
) | |
} | |
export default () => ( | |
<CartProvider> | |
<Sku /> | |
</CartProvider> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use this file as a component, it'd be something like this, right?
If you wanted to pass props, you could modify the default export of
sku.js
like so:And then use it like so:
Does that help?