I was trying to call a REST webservice to fetch data from the database and return a JSON object back to the ReactJS app.When I access the URL from the browser it displays the JSON object but when I try to render the data in the table no content is displayed.
My sample code looks like
import React from 'react';
import $ from 'jquery';
export default class Members extends React.Component {
constructor(props) {
super(props);
this.state = { users: [] };
}
componentDidMount() {
$.ajax({
url: "http://localhost:8080/users"
}).then(function(data) {
this.setState({
users:data
});
});
}
render() {
return (
<UsersList users={this.state.users}/>
)
}
}
class UsersList extends React.Component {
render() {
var users = this.props.users.map(users =>
<User key={user._links.self.href} user={user}/>
);
return (
<table>
<tbody>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
{users}
</tbody>
</table>
)
}
}
class User extends React.Component {
render() {
return (
<tr>
<td>{this.props.user.firstName}</td>
<td>{this.props.user.lastName}</td>
</tr>
)
}
}
Is this a problem of asynchronous loading ?