Skip to content

Instantly share code, notes, and snippets.

@hanipcode
Created March 26, 2019 09:47
Show Gist options
  • Save hanipcode/98a31a3504140d9bdb9aa7730a3269c5 to your computer and use it in GitHub Desktop.
Save hanipcode/98a31a3504140d9bdb9aa7730a3269c5 to your computer and use it in GitHub Desktop.
class ListingCard extends React.Component {
state = {
favoriteListings : [],
}
async componentDidMount() {
const { listingId } = this.props;
const favoriteListings = await getData();
this.setState({ favoriteListings });
}
onFavoriteClick(listingId) {
const { favoriteListings } = this.state;
await callServerApi(listingId);
const favoriteListings = favoriteListings.push(listingId);
this.setState({ favoriteListings });
}
render() {
const { listingId } = this.props;
const { favoriteListings } = this.state;
const isFavorite = favoriteListings.indexOf(listingId) > -1;
return (
<ListingCard isFavorite={isFavorite} onHeartClick={onFavoriteClick} />
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment