Using axios with function components

In this section, we'll implement REST API calls using axios in a function component. We'll refactor the App component we built in the last section:

  1. First, we are going to declare a constant, called defaultPosts that is going to hold the default posts state we'll use a little later. We'll add this after the IPost interface and set this to an empty array:
const defaultPosts: IPost[] = [];
  1. We'll remove the IState interface because the state will be structured as individual pieces of state now.
  2. We'll also remove the previous App class component.
  3. Next, let's start the App function component under the defaultPosts constant:
const App: React.SFC = () => {}
  1. We can now create the state for the posts, error, ...

Get Learn React with TypeScript 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.