import React, { Component } from 'react';
class UserList extends Component {
constructor(props) {
super(props);
this.state = {
users: [],
error: null
};
}
componentDidMount() {
fetch('YOUR_API_URL')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
this.setState({ users: data });
})
.catch(error => {
this.setState({ error: error.message });
});
}
render() {
const { users, error } = this.state;
if (error) {
return <div>Error: {error}</div>;
}
return (
<div>
<h1>User List</h1>
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
);
}
}
export default UserList;