Skip to content

Instantly share code, notes, and snippets.

@nikola-bodrozic
Created May 12, 2019 12:20
Show Gist options
  • Select an option

  • Save nikola-bodrozic/58ad2393983141766aef459b42c21911 to your computer and use it in GitHub Desktop.

Select an option

Save nikola-bodrozic/58ad2393983141766aef459b42c21911 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import axios from "axios";
class Comments extends Component {
constructor(props) {
super(props);
this.state = {
postId: '',
commentBody:''
};
}
componentDidMount() {
this.getComments();
}
getComments = async () => {
const res = await axios.get("http://localhost:3005/posts/1");
const data = await res.data;
const id = data.id
this.setState({ postId: data.id });
const res2 = await axios.get("http://localhost:3005/comments/"+id);
const data2 = await res2.data;
this.setState({ commentBody: data2.body });
};
render() {
return <p>post id <strong>{this.state.postId}</strong> and its comment <strong>{this.state.commentBody}</strong></p>;
}
}
export default Comments;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment