Skip to content

Instantly share code, notes, and snippets.

@techomoro

techomoro/App.js Secret

Created January 16, 2021 05:15
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 techomoro/3b0cef5ea2baad7535535e2346ecdd64 to your computer and use it in GitHub Desktop.
Save techomoro/3b0cef5ea2baad7535535e2346ecdd64 to your computer and use it in GitHub Desktop.
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