concept Letters Social API in category react

This is an excerpt from Manning's book React in Action.
Listing 4.9. Fetching data when the App component mounts
//... constructor(props) { //... this.getPosts = this.getPosts.bind(this); #1 } componentDidMount() { this.getPosts(); #1 } componentDidCatch(err, info) { #2 console.error(err); #2 console.error(info); this.setState(() => ({ #2 error: err })); } getPosts() { API.fetchPosts(this.state.endpoint) #3 .then(res => { return res .json() #4 .then(posts => { const links = parseLinkHeader(res.headers.get('Link')); #5 this.setState(() => ({ posts: orderBy(this.state.posts.concat(posts), 'date', 'desc'), #6 endpoint: links.next.url #7 })); }) .catch(err => { this.setState(() => ({ error: err })); #8 }); }); } render() { //... <button className="block" onClick={this.getPosts}> #9 Load more posts </button> //... } //... #1 Bind class method and use it to fetch posts from the API when the component mounts. #2 Set up an error boundary for the app so you can handle errors. #3 Fetch posts using the included API module. #4 The API module uses the Fetch API, so you need to unwrap the JSON response. #5 The Letters Social API returns pagination information in headers, so you can use the parse-link-header to pull the URL for the next page of posts out. #6 Add the new posts to state and ensure they’re sorted correctly. #7 Update the endpoint state. #8 If there’s an error, update component state. #9 Now that you have it defined, assign the getPosts method to the load more event handler. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":74},{\"line\":3,\"ch\":76}],[{\"line\":7,\"ch\":74},{\"line\":7,\"ch\":76}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":3,\"ch\":74},{\"line\":3,\"ch\":76}],[{\"line\":7,\"ch\":74},{\"line\":7,\"ch\":76}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":17,\"ch\":74},{\"line\":17,\"ch\":76}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":20,\"ch\":74},{\"line\":20,\"ch\":76}],[{\"line\":46,\"ch\":0},{\"line\":46,\"ch\":2}],[{\"line\":22,\"ch\":74},{\"line\":22,\"ch\":76}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":25,\"ch\":74},{\"line\":25,\"ch\":76}],[{\"line\":48,\"ch\":0},{\"line\":48,\"ch\":2}],[{\"line\":26,\"ch\":74},{\"line\":26,\"ch\":76}],[{\"line\":49,\"ch\":0},{\"line\":49,\"ch\":2}],[{\"line\":30,\"ch\":74},{\"line\":30,\"ch\":76}],[{\"line\":50,\"ch\":0},{\"line\":50,\"ch\":2}],[{\"line\":36,\"ch\":74},{\"line\":36,\"ch\":76}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}]]"} !@%STYLE%@!
With those logistical matters out of the way, you should add the ability to fetch post to the App component. To do that, you’ll need to send a network request to the Letters Social API using the Fetch API (bundled up in the API module you pulled in). Right now, your component doesn’t do much. You haven’t defined any lifecycle methods outside of the constructor and render methods, so the component doesn’t have any data to work with. You need to fetch data from the API and then update component state with that data. You’ll also add an error boundary so that if your component encounters an error you can show an error message instead of the entire app unmounting. The next listing shows how to add the class methods to the App component.
Listing 4.9. Fetching data when the App component mounts
//... constructor(props) { //... this.getPosts = this.getPosts.bind(this); #1 } componentDidMount() { this.getPosts(); #1 } componentDidCatch(err, info) { #2 console.error(err); #2 console.error(info); this.setState(() => ({ #2 error: err })); } getPosts() { API.fetchPosts(this.state.endpoint) #3 .then(res => { return res .json() #4 .then(posts => { const links = parseLinkHeader(res.headers.get('Link')); #5 this.setState(() => ({ posts: orderBy(this.state.posts.concat(posts), 'date', 'desc'), #6 endpoint: links.next.url #7 })); }) .catch(err => { this.setState(() => ({ error: err })); #8 }); }); } render() { //... <button className="block" onClick={this.getPosts}> #9 Load more posts </button> //... } //... #1 Bind class method and use it to fetch posts from the API when the component mounts. #2 Set up an error boundary for the app so you can handle errors. #3 Fetch posts using the included API module. #4 The API module uses the Fetch API, so you need to unwrap the JSON response. #5 The Letters Social API returns pagination information in headers, so you can use the parse-link-header to pull the URL for the next page of posts out. #6 Add the new posts to state and ensure they’re sorted correctly. #7 Update the endpoint state. #8 If there’s an error, update component state. #9 Now that you have it defined, assign the getPosts method to the load more event handler. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":74},{\"line\":3,\"ch\":76}],[{\"line\":7,\"ch\":74},{\"line\":7,\"ch\":76}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":3,\"ch\":74},{\"line\":3,\"ch\":76}],[{\"line\":7,\"ch\":74},{\"line\":7,\"ch\":76}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":9,\"ch\":74},{\"line\":9,\"ch\":76}],[{\"line\":10,\"ch\":74},{\"line\":10,\"ch\":76}],[{\"line\":12,\"ch\":74},{\"line\":12,\"ch\":76}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":17,\"ch\":74},{\"line\":17,\"ch\":76}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":20,\"ch\":74},{\"line\":20,\"ch\":76}],[{\"line\":46,\"ch\":0},{\"line\":46,\"ch\":2}],[{\"line\":22,\"ch\":74},{\"line\":22,\"ch\":76}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":25,\"ch\":74},{\"line\":25,\"ch\":76}],[{\"line\":48,\"ch\":0},{\"line\":48,\"ch\":2}],[{\"line\":26,\"ch\":74},{\"line\":26,\"ch\":76}],[{\"line\":49,\"ch\":0},{\"line\":49,\"ch\":2}],[{\"line\":30,\"ch\":74},{\"line\":30,\"ch\":76}],[{\"line\":50,\"ch\":0},{\"line\":50,\"ch\":2}],[{\"line\":36,\"ch\":74},{\"line\":36,\"ch\":76}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}]]"} !@%STYLE%@!