I want fetch a json api and push that result into an array:
import React from 'react';
import ReactDOM from 'react-dom';
function Users(){
const url = 'https://randomuser.me/api/?results=5';
let nodes = [];
fetch(url)
.then(response => {
return response.json();
})
.then(j => {
for( var i = 0; i <= j.results.length; i++ ){
nodes.push(<li>{j.results[i].name.first}</li>);
}
});
return(
<ul>{nodes}</ul>
);
}
ReactDOM.render(
<Users />,
document.getElementById('main')
);
But I have the following error in the console:
TypeError: j.results[i] is undefined
How can I fixed this error?
i <= j.results.length;should bei < j.results.length;but you structuring is not correct