Answers for "reactjs loop through api response in react"

0

reactjs loop through api response in react

import React from 'react';

class Users extends React.Component {
  constructor() {
    super();
    this.state = { users: [] };
  }

  componentDidMount() {
    fetch('/api/users')
      .then(response => response.json())
      .then(json => this.setState({ users: json.data }));
  }

  render() {
    return (
      <div>
        <h1>Users</h1>
        {
          this.state.users.length == 0
            ? 'Loading users...'
            : this.state.users.map(user => (
              <figure key={user.id}>
                <img src={user.avatar} />
                <figcaption>
                  {user.name}
                </figcaption>
              </figure>
            ))
        }
      </div>
    );
  }
}

ReactDOM.render(<Users />, document.getElementById('root'));
Posted by: Guest on February-09-2020
0

loop through api response in react

renderData() {
    if (!this.state.trails) {
        return null;
    } 
    return this.state.trails.map(trail => <p>{trail.name}</p>);
}
Posted by: Guest on February-26-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language