I'm relatively new to JavaScript / React.js. I have a random API from which I want to read an item (in this example an email adress).
What if the array has more emails for different users?
In that case, how can I call that specific email, let's say 2nd and 5th. What is the syntax for that?
Code is given bellow:
import {useState,useEffect} from 'react';
function App(){
const url = 'https://randomuser.me/api/';
const [information,setInformation]=useState([]);
async function getData(){
const resp = await fetch(url);
const data = await resp.json();
console.log(data);
const [item]=data.results;
setInformation(item);
}
useEffect(()=>{
getData();
},[])
return(
<>
{information.email}
</>
)
}
export default App;
Since you are getting the data as an array, of the format
email: ["a@gmail.com", "b@gmail.com"]
You can map them as :
export default function App() {
let information = { email: ["a@gmail.com", "b@gmail.com"] };
return (
<ul>
{information.email.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
);
}
Or to display a particular email, say of 1st location, access them as:
<>
{information.email[1]}
</>