Created
June 9, 2020 20:24
-
-
Save marshallmurphy/10c9de08aa2af1c2a10d362f9bf92a36 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
// App.js | |
import React, { useEffect } from 'react' | |
import { useDispatch, useSelector } from 'react-redux' | |
import { fetchRecipes, recipesSelector } from './slices/recipes' | |
const App = () => { | |
const dispatch = useDispatch() | |
const { recipes, loading, hasErrors } = useSelector(recipesSelector) | |
useEffect(() => { | |
dispatch(fetchRecipes()) | |
}, [dispatch]) | |
// error handling & map successful query data | |
const renderRecipes = () => { | |
if (loading) return <p>Loading recipes...</p> | |
if (hasErrors) return <p>Cannot display recipes...</p> | |
return recipes.map(recipe => | |
<div key={recipe.idMeal} className='tile'> | |
<h2>{recipe.strMeal}</h2> | |
<img src={recipe.strMealThumb} alt=''/> | |
</div> | |
) | |
} | |
return ( | |
<section> | |
<h1>Recipes</h1> | |
<div className='content'> | |
{renderRecipes()} | |
</div> | |
</section> | |
) | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment