Skip to content

Instantly share code, notes, and snippets.

@paigen11
Last active March 1, 2020 22:28
Show Gist options
  • Save paigen11/944ec06b8ce99f6b477a3ffd31b866c2 to your computer and use it in GitHub Desktop.
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.
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