reactjs - Consume RESTful data with React and Axios -


how can retrieve data https://api.github.com/search/users?q=jason , render in react? tried like:

constructor(){   this.state = {     data: []               }; }  componentdidmount(){     axios.get("https://api.github.com/search/users?q="+_searchterm)                 .then(res => {                     this.setstate({                         data: _.values(res.data.items)                     })                 });  }   render() {   const listproducts = this.state.data.map((product) =>      <li key={product.tostring()}>{product}</li>   );    return (     <div>       <ul>{listproducts}</ul>          </div>    ); } 

but didn't work. error message:

unhandled rejection (invariant violation): objects not valid react child (found: object keys {login, id, avatar_url, gravatar_id, ...). if meant render collection of children, use array instead or wrap object using createfragment(object) react add-ons.

so guess have convert response array. not sure how it.

share|improve question
up vote 1 down vote accepted

you returning object rather component or string.

change this;

const listproducts = this.state.data.map((product) =>      <li key={product.tostring()}>{product}</li> ); 

to this

// need set key unique string id const listproducts = this.state.data.map((product) =>      <li key={product.id}>{json.stringify(product)}</li> ); 
share|improve answer
    
thank you, works! – jason sep 11 @ 13:24

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments