Last active
March 1, 2020 22:28
-
-
Save paigen11/944ec06b8ce99f6b477a3ffd31b866c2 to your computer and use it in GitHub Desktop.
A simple example showing a child component consuming the context provided by a parent with the useContext hook in a React application.
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
import React, { useContext } from 'react'; | |
import { orderItems } from '../../../../services/orderService'; | |
import CheckoutDrawerContext from '../../../../context/CheckoutDrawerContext'; | |
export const CheckoutSummaryDetails = props => { | |
const { summaryDetails, userInfo, items } = props; | |
const checkoutDrawer = useContext(CheckoutDrawerContext); | |
const placeOrder = async () => { | |
const executionResponse = await orderItems(userInfo, items); | |
if (executionResponse === 'successfully placed order') { | |
checkoutDrawer.toggleCheckoutDrawer(); | |
} | |
}; | |
return ( | |
<div className="cart-drawer__summary"> | |
<strong className="cart-drawer__header">Summary</strong> | |
<div className="cart-drawer__summary-content"> | |
<div className="checkout-summary"> | |
<span>Final Checkout Summary</span> <span>{summaryDetails}</span> | |
</div> | |
</div> | |
<button type="button" className="order-button" onClick={placeOrder}> | |
Order Now | |
</button> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment