Last active
May 26, 2020 20:45
-
-
Save Omkaragrawal/44018029d869843b36f1b41ad22db884 to your computer and use it in GitHub Desktop.
For Question --> https://stackoverflow.com/questions/62029882/how-to-map-over-a-response-from-a-rest-call#62029882. You can see the execution here -> https://bit.ly/stackoverflow-react-first-repl. I would recommend and illustrate using the useState hook only.
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
import React, { useEffect, useState } from 'react'; | |
import axios from 'axios'; | |
const App = () => { | |
let [list, setList] = useState(<>LOADING</>); | |
useEffect(() => { | |
// You can use your link here | |
// I have created corsenabled.herokuapp.com just to bypass the CORS issue. It's only for testing and educational purpose only. No intention to infringe any copyrights or other legal matters | |
// I have used jsonplaceholder.typicode.com as an example | |
axios.get('https://corsenabled.herokuapp.com/get?to=https://jsonplaceholder.typicode.com/posts') | |
.then(response => { | |
let tempData = response.data; | |
let anotherData = tempData.map(data => { | |
return (<div>{data.userId}<br/>{data.id}<br/>{data.title}<br/>{data.body} <br/><br/></div>) | |
}) | |
// tempData = tempData.map(data => <div> {JSON.stringify(data)} </div>) | |
console.log(tempData) | |
setList(anotherData) | |
}) | |
.catch(error => { | |
console.log(error) | |
}) | |
}, []) | |
return ( | |
<> | |
<div className='container'> | |
<div className='row'> | |
{ | |
list | |
} | |
</div> | |
</div> | |
</> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment