@@ -2,7 +2,9 @@ import React ,{useEffect, useState} from 'react'
2
2
import axios from 'axios'
3
3
import { toast } from 'react-toastify'
4
4
import ClipLoader from 'react-spinners/ClipLoader' ;
5
+ import { posts } from '../homePage/data' ;
5
6
7
+ //Sample url(local env) - https://newsapi.org/v2/top-headlines?country=us&apiKey=6004b8fcb1604003b4ead57854e8d2c2
6
8
function FetchApi ( ) {
7
9
const [ data , setData ] = useState ( [ ] ) ;
8
10
const [ isLoading , setIsLoading ] = useState ( false ) ;
@@ -12,9 +14,9 @@ function FetchApi() {
12
14
//sample get api
13
15
try {
14
16
setIsLoading ( true ) ;
15
- await axios . get ( "https://newsapi.org/v2/top-headlines?country=us&apiKey=6004b8fcb1604003b4ead57854e8d2c2 " )
17
+ await axios . get ( "https://api.thecatapi.com/v1/images/search?limit=10 " )
16
18
. then ( ( response ) => {
17
- setData ( response ?. data ?. articles ) ;
19
+ setData ( response ?. data ) ;
18
20
} )
19
21
} catch ( e ) {
20
22
console . error ( e ) ;
@@ -37,19 +39,19 @@ function FetchApi() {
37
39
return (
38
40
< >
39
41
< div className = "btn-fetchContainer" >
40
- < button className = 'btn-fetch' onClick = { data . length > 0 ? showToast : fetchData } > Click here to Fetch Api Data!!</ button >
42
+ < button className = 'btn-fetch' onClick = { data ? .length > 0 ? showToast : fetchData } > Click here to Fetch Api Data!!</ button >
41
43
</ div >
42
44
43
45
< div className = "ui-fetched" >
44
46
{
45
47
data . map ( ( value , index ) => {
46
- const imageUrl = value ?. urlToImage ;
48
+ const imageUrl = value ?. url ;
47
49
return (
48
50
< div className = 'card' style = { { width : "20rem" } } key = { index } >
49
51
< img src = { ( imageUrl && imageUrl !== null ) ? imageUrl : thumbImage } className = "card-img-top" width = { 300 } alt = { value ?. title } />
50
52
{ imageUrl !== null && < div className = "card-body" >
51
- < h5 className = "card-title" > { value ?. title } </ h5 >
52
- < p className = "card-desc" > { value ?. description } </ p > < br > </ br >
53
+ < h5 className = "card-title" > { value ?. id } </ h5 >
54
+ < p className = "card-desc" > { posts [ 0 ] ?. desc } </ p > < br > </ br >
53
55
Want to Read more, < a href = { value ?. url || "/notfound" } target = '_blank' rel = 'noopener noreferrer' className = "btn-primary" > < span style = { { fontWeight : "bold" } } > Click here</ span > </ a >
54
56
</ div > }
55
57
</ div >
0 commit comments