Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DataTable extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- error: null,
- isLoaded: false,
- items: []
- };
- }
- componentDidMount() {
- fetch("/data")
- .then(res => res.json())
- .then(
- (result) => {
- this.setState({
- isLoaded: true,
- items: result
- });
- },
- (error) => {
- this.setState({
- isLoaded: true,
- error
- });
- }
- )
- }
- render() {
- const { error, isLoaded, items } = this.state;
- if (error) {
- return <div>Error: {error.message}</div>;
- } else if (!isLoaded) {
- return <div>Loading...</div>;
- } else {
- return (
- <div>
- <table>
- <tbody>
- {items.map(item => (
- <tr key={item.id}>
- <td>
- {item.name} {item.age} {item.phone} {item.email} {item.location}
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- <AddForm />
- </div>
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement