Skip to content

Instantly share code, notes, and snippets.

@marshallmurphy
Created June 9, 2020 20:24
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 marshallmurphy/10c9de08aa2af1c2a10d362f9bf92a36 to your computer and use it in GitHub Desktop.
Save marshallmurphy/10c9de08aa2af1c2a10d362f9bf92a36 to your computer and use it in GitHub Desktop.
// 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