Skip to content

Instantly share code, notes, and snippets.

@fcsonline
Last active December 2, 2016 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcsonline/236a12e570c253d0547d7ae541a59880 to your computer and use it in GitHub Desktop.
Save fcsonline/236a12e570c253d0547d7ae541a59880 to your computer and use it in GitHub Desktop.
Cart - CartList
class CartList extends React.Component {
onClickItem(event, index) {
this.state.setState({
selected: [
...this.state.selected,
index
]
});
}
onClickRemove () {
// Go to your store and remove item
}
renderCartItem(item, index) {
const isPremium = item.premium ? 'premium' : '';
const isSelected = this.state.selected.indexOf(index) >= 0 ? 'selected' : '';
return (
<li className={`${isPremium} ${isSelected}`} onClick={(event) => this.onClickItem(event, index)}>
<header>{item.title}</header>
<p>{item.description}</p>
<button onClick={this.onClickRemove}>Remove from cart</button>
</li>
);
}
render() {
const { items } = this.props;
return (
<ul className='cart'>
{items.map((item, index) => this.renderCartItem(item, index)}
</ul>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment