Last active
January 29, 2018 22:57
-
-
Save puzzfuzz/5cb9fee56e3c2b4741fdf70a97214b03 to your computer and use it in GitHub Desktop.
This file contains 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
// CartController.jsx | |
@CartContainer | |
export default class CartController extends Component { | |
static propTypes = { | |
cart: PropTypes.object.isRequired, | |
adjustQuantity: PropTypes.func.isRequired | |
}; | |
render() { | |
const { cart, adjustQuantity } = this.props; | |
return ( | |
<div className="shopping-cart"> | |
{cart.items.map( item => | |
<CartItem key={item.id} item={item} adjustQuantity={adjustQuantity}/> | |
)} | |
{/* Quantity update is rendered via a Portal component, | |
which the parent component doesn't know or care about */} | |
<CartQuantityPortal cart={cart} /> | |
</div> | |
) | |
} | |
} | |
// CartQuantityPortal.jsx | |
function CartCountPortal({ cart }) { | |
const itemCount = cart.items.length; | |
// createPortal signature is identical to ReactDOM.render | |
return ReactDOM.createPortal( | |
<span className=“cart-qauantity__count">{itemCount}</span>, | |
document.getElementById('cart-qauantity’) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment