-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from shubhamgautam89/shubhamgautam89-patch-1
Shubhamgautam89 patch 1
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Ideally, we want to enforce separation of concerns by separating this process into two parts: | ||
* Presentational Components: Components that care about how data is shown to the user. In this example, that's the rendering the list of dog images. | ||
* Container Components: Components that care about what data is shown to the user. In this example, that's fetching the dog images. | ||
* In many cases the container component can be replaced wiith Hooks, for this example we wil be using hook as container. | ||
*/ | ||
|
||
import { useState } from "react"; | ||
|
||
export default function useDogImages() { | ||
const [dogs, setDogs] = useState([]); | ||
|
||
useEffect(() => { | ||
fetch("https://dog.ceo/api/breed/labrador/images/random/6") | ||
.then((res) => res.json()) | ||
.then(({ message }) => setDogs(message)); | ||
}, []); | ||
|
||
return dogs; | ||
} | ||
|
||
export function DogImages() { | ||
const dogs = useDogImages(); | ||
|
||
return dogs.map((dog) => <img src={dog} key={i} alt="Dog" />); | ||
} |
Empty file.