Created
May 27, 2020 15:53
-
-
Save Aperyon/0ca3be74babe04cd17810f1bf0ad60d6 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 React from "react"; | |
// Can not really write good unit tests | |
// Every test should start with a mock of the API call to fetch the `book` | |
export default function BookDetail() { | |
const [isDeleteClicked, setDeleteClicked] = React.useState(false); | |
const { state: book, fetchBook, deleteBook } = React.useContext(BookContext); | |
const bookUrl = utils.buildBookUrlFromUri(); | |
React.useEffect(() => { | |
fetchBook(bookUrl); | |
}, []); | |
function onDeleteClick() { | |
console.log("Book Deleted"); | |
deleteBook(bookUrl); | |
setDeleteClicked(true); | |
} | |
if (book === null) { | |
return <p>Loading...</p>; | |
} | |
return ( | |
<> | |
<p>Book: {book.title}</p> | |
<button onClick={onDeleteClick}>Delete book</button> | |
<p>Is delete clicked: {isDeleteClicked}</p> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment