Created
February 18, 2020 12:28
-
-
Save vikrantnegi/ae46d31894cf6b0eb5814aceaffbc744 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
export default function App() { | |
const [books, setBooks] = useState([...new Array(10).fill({})]); | |
const [isDataFetched, setDataFetched] = useState(false); | |
useEffect(() => { | |
fetch( | |
"https://www.googleapis.com/books/v1/volumes/?maxResults=30&q=danbrown" | |
) | |
.then(response => response.json()) | |
.then(responseJson => { | |
const { items } = responseJson; | |
const booksList = items.map(book => { | |
const { | |
volumeInfo: { title, authors, imageLinks }, | |
id: bookId | |
} = book; | |
return { | |
bookId, | |
thumbnail: imageLinks | |
? imageLinks.thumbnail | |
: "https://i.ibb.co/YLC0nQQ/not-found.png", | |
title, | |
authors: authors ? authors.toString().replace(/,/g, ", ") : "-" | |
}; | |
}); | |
setBooks(booksList); | |
setDataFetched(true); | |
}) | |
.catch(error => { | |
console.error(error); | |
}); | |
}, []); | |
const renderBookComponent = ({ item }) => { | |
const { thumbnail, title, authors, bookId } = item; | |
return ( | |
<BookCardComponent | |
key={bookId} | |
title={title} | |
authors={authors} | |
thumbnail={thumbnail} | |
/> | |
); | |
}; | |
const renderX = () => ( | |
<FlatList | |
data={books} | |
renderItem={renderBookComponent} | |
keyExtractor={item => item.bookId} | |
/> | |
); | |
const renderPlaceholders = () => | |
books.map((e, i) => <BookCardPlaceholder key={i} />); | |
return ( | |
<SafeAreaView style={styles.container}> | |
{isDataFetched ? renderX() : renderPlaceholders()} | |
</SafeAreaView> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment