The use of a FlatList is usually a sign of a complex UI, since FlatList is made to display large amounts of data in an efficiently looking way. To use this component, we first need to get the user data from an API endpoint. We will use the axios library to fetch data from this endpoint.
In this example we are using a fetch() function to call the name of the endpoint and the API Key of that particular endpoint from the API library. The function returns a promise object that carries promise property. To get the value from the response object, we use the json() method.
Like React Native, one of the React Native ways to do this is to use a FlatList to display the data. This is generally done when the amount of data is too large to be rendered in a list view ( i.e, there are too many elements to accommodate on the screen at a time). To use a FlatList with react-native, we first import the FlatList component as a react-native component. Then, we initialize a FlatList with a flatlist prop and the array of items to render. We also need to set the initial data to the FlatList.
Since our application deals with user data, like name, profile, images and so on, we need to fetch them from the server. It's the perfect time to use fetch(endpoint, apiKey). Below I have a function which fetches the user information from an API endpoint and caches it. We can call this function at any point to get data from the API. The values of headers, credentials and params passed in this func- tion depend on the API endpoint we are accessing. d2c66b5586