Skip to content

Instantly share code, notes, and snippets.

@puzzfuzz
Last active January 29, 2018 22:57
Show Gist options
  • Save puzzfuzz/5cb9fee56e3c2b4741fdf70a97214b03 to your computer and use it in GitHub Desktop.
Save puzzfuzz/5cb9fee56e3c2b4741fdf70a97214b03 to your computer and use it in GitHub Desktop.
// 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