-
-
Save techomoro/3b0cef5ea2baad7535535e2346ecdd64 to your computer and use it in GitHub Desktop.
This file contains 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
import "./App.css"; | |
import React, { Component } from "react"; | |
import Library from "./components/Library"; | |
import AppContext from "./AppContext"; | |
class App extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
books: [ | |
{ | |
_id: "1", | |
title: "Book name 1", | |
description: "Best book for business man, students and entreprenuers", | |
author: "Rahul", | |
}, | |
{ | |
_id: "2", | |
title: "Book name 2", | |
description: "Best book for business man, students and entreprenuers", | |
author: "Rahul", | |
}, | |
{ | |
_id: "3", | |
title: "Book name 3", | |
description: "Best book for business man, students and entreprenuers", | |
author: "Rahul", | |
}, | |
{ | |
_id: "4", | |
title: "Book name 4", | |
description: "Best book for business man, students and entreprenuers", | |
author: "Rahul", | |
}, | |
], | |
favorites: [], | |
}; | |
} | |
addToFavorites = (book) => { | |
if (!this.state.favorites.includes(book)) { | |
this.setState({ | |
favorites: [...this.state.favorites, book], | |
}); | |
} | |
}; | |
removeFromFavorites = (book) => { | |
this.state.favorites.map((favorite, index) => { | |
console.log(favorite._id, book._id); | |
if (favorite._id == book._id) { | |
this.state.favorites.splice(index, 1); | |
} | |
}); | |
this.setState({ | |
favorites: this.state.favorites, | |
}); | |
}; | |
render() { | |
return ( | |
<AppContext.Provider | |
value={{ | |
state: this.state, | |
addToFavorites: this.addToFavorites, | |
removeFromFavorites: this.removeFromFavorites, | |
}} | |
> | |
<Library /> | |
</AppContext.Provider> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment