Created
March 26, 2019 09:43
-
-
Save hanipcode/6790ef23c9e2cbc6d892db14e5330c19 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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