import React from 'react'; import $ from 'jquery'; import ProjectListItem from './ProjectListItem'; class ProjectList extends React.Component { constructor(){ super(); this.state = { loading: true, error: "", projects: null } } componentDidMount(){ // Load projects from API this.serverRequest = $.getJSON(this.props.source, json => { if (json.results){ this.setState({ projects: json.results, loading: false }); }else{ this.setState({ error: `Invalid JSON response: ${JSON.stringify(json)}`, loading: false }); } }) .fail((jqXHR, textStatus, errorThrown) => { this.setState({ error: `Could not load projects list: ${textStatus}`, loading: false }); }) } componentWillUnmount(){ this.serverRequest.abort(); } render() { if (this.state.loading){ return (
Loading projects...
); } else if (this.state.projects){ return (); }else if (this.state.error){ return (
An error occurred: {this.state.error}
); }else{ return (
); // should never happen } } } export default ProjectList;