Skip to content

Instantly share code, notes, and snippets.

@dilantha111
Created August 24, 2020 17:19
Show Gist options
  • Save dilantha111/56999d3372a63a924e5e20c71f46cc43 to your computer and use it in GitHub Desktop.
Save dilantha111/56999d3372a63a924e5e20c71f46cc43 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import { getMetalGenres } from './services/music-genres';
import './App.css';
function App() {
const [genreList, setGenreList] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const handleFetchMetal = async () => {
try {
setGenreList(null);
setIsLoading(true);
const genres = await getMetalGenres();
setGenreList(genres);
} catch (error) {
setError(error);
} finally {
setIsLoading(false);
}
};
return (
<div className="App">
<h1> Metal Music Genres </h1>
<div className="btn-container">
<button onClick={handleFetchMetal}> Fetch Some Metal Music !!!!</button>
</div>
{error ? <span> Error: {error}</span> : null}
{isLoading ? <span> Loading ... </span> : null}
{genreList ? <GenreList genres={genreList} /> : null}
</div>
);
}
function GenreList(props) {
return (
<ul className="genre-list">
{
props.genres.map(genre => (
<li key={genre} > {genre} </li>
))
}
</ul>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment