Answer the question
In order to leave comments, you need to log in
How to output all name from objects in array (jsonplaceholder)?
There is a link:
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(json => console.log(json))
import React, { Component } from 'react';
import './App.css';
function req1() {
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(response => response.json())
.then(json => {
var name = json.name
var body = json.username;
var email = json.email;
var website = json.website;
document.getElementById("printUser").innerHTML =
'<td class="firstCol">1.</td>' + '<td>' + name + '</td>'
+ '<td>' + body + '</td>'
+ '<td>' + email + '</td>'
+ '<td>' + website + '</td>';
});
}
req1();
class App extends Component {
render() {
return(
<div className="App">
<table>
<tbody>
<th>json users</th>
<tr id="printUser">
</tr>
</tbody>
</table>
</div>
)
}
}
export default App;
Answer the question
In order to leave comments, you need to log in
class App extends Component {
state = {
users: []
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(users => this.setState({ users }));
}
render() {
return(
<div className="App">
<table>
<tbody>
{this.state.users.map(n => (
<tr key={n.id}>
<td>{n.name}</td>
<td>{n.username}</td>
<td>{n.email}</td>
<td>{n.website}</td>
</tr>
))}
</tbody>
</table>
</div>
)
}
}
I recommend that before you start writing anything, it is good to study the possibilities of the library or at least read the Tutorial and the Main Concepts and Advanced Guides sections of the official documentation .
What you want to implement correctly is done like this .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question