We import the necessary components from React Native: View and Text, as well as axios for making HTTP requests and Pokemon for representing a Pokemon object.
We define our component class, PokemonList, and create a constructor method to set the initial state of our component with an empty array of Pokemon objects, a loading flag, and an error flag.
We use the componentDidMount lifecycle method to make an HTTP request to the Pokemon API using axios. We then map over the results to create a new Pokemon object for each result and set the state with the list of Pokemon objects.
We check the loading and error flags in the render method and return appropriate messages if needed.
Finally, we render a View component that contains a list of Text components with the name of each Pokemon.
Open the App.js file in the project root directory and replace its contents with the following code:
importReactfrom 'react';
import { StyleSheet, View } from 'react-native';
importPokemonListfrom './src/PokemonList';
exportdefaultfunctionApp() {
return (
<View style={styles.container}>
<PokemonList />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
This file creates a new component App that renders our PokemonList component.
In your terminal, navigate to the project root directory and run the following command to start the development server:
expo start
This will open a browser window showing the Expo Developer Tools.
To test your app on your Android or iOS device or emulator, you need to install the Expo app from Google Play Store or App Store. Once installed, open the app and scan the QR code on the Expo Developer Tools page using your device camera.
Expo will then build and deploy your app to your device or emulator.
That's it! You have successfully created a React Native Pokedex app that uses the Pokemon API. You have also learned how to use state and props to create dynamic and responsive components in React. Keep exploring and experimenting with React Native and Expo to create more complex and interesting apps.