Skip to content

Instantly share code, notes, and snippets.

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