Skip to content

Instantly share code, notes, and snippets.

@olajohn-ajiboye
Created July 4, 2019 15:17
Show Gist options
  • Save olajohn-ajiboye/55a7ed037f0113e7475a95b771553222 to your computer and use it in GitHub Desktop.
Save olajohn-ajiboye/55a7ed037f0113e7475a95b771553222 to your computer and use it in GitHub Desktop.
UseEffect experiment
import React, { useState, useEffect } from 'react';
import RecipeList from './components/RecipeList'
import RecipeDetails from './components/RecipeDetails'
function App() {
const apiKey = `36920f6651c9cd9d91a6c3205cabaa19`
let url = `https://www.food2fork.com/api/search?key=${apiKey}`
const [showHomeButton, setShowHomeButton] = useState(false)
const [recipes, setRecipes] = useState([])
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState('')
const fetchRecipe = async () => {
const recipeData = await fetch(url)
const { recipes } = await recipeData.json()
setRecipes(recipes)
setLoading(false)
console.log("I am beign rendered again and again")
}
useEffect(() => {
fetchRecipe()
})
return (
<div>
{loading ? <h1 className="text-center">…fetching {search} Recipe</h1> :
<RecipeList recipes={recipes} />}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment