To run this code:
-
Install dependencies:
npm i
-
Start the development server:
npm run dev
You can also view the completed application here:
React Pokemon Tutorial
This code is complete. To create a new Vite project and follow along, use the steps below:
-
Create a Vite App
-
Install dependencies:
npm install
-
View scripts in
package.jsonto understand how to run the template app. -
Prepare files:
- Empty
App.jsx,App.css, andindex.css. - Copy the completed
index.cssfrom this repository and ensure it's connected to yourmain.jsx.
- Empty
-
Create the
Appcomponent- Use the RFC snippet from the React Snippets VS Code extension (optional).
-
Create a static
Cardcomponent- Populate the
Appcomponent with a couple of static<Card>components.
- Populate the
-
Fetch Data
- Create a function called
fetchData()to perform a fetch request to a static URL. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_FetchExample:https://pokeapi.co/api/v2/pokemon/25
- Create a function called
-
Parse Data
- Extract the following from the API response:
data.namedata.spritedata.id
- Extract the following from the API response:
-
Manage State
- Use
useStateto create ateamarray.
- Use
-
Fetch Random Pokemon
- For each fetch request, search using a random index (1–151 for Generation 1 Pokémon) and append the new data to the
teamarray.
- For each fetch request, search using a random index (1–151 for Generation 1 Pokémon) and append the new data to the
-
Render Cards Dynamically
- Use
team.map()to display a uniqueCardfor each Pokémon in theteam.- Pass a unique value for each
key. - Pass the Pokémon data into the
Cardcomponent.
- Pass a unique value for each
- Use
-
Dynamic Card Rendering
- Ensure the
Cardcomponent renders dynamic data.
- Ensure the
-
Add Buttons
- Fetch Button: Fetches a new Pokémon and adds it to the team.
- Clear Button: Resets the
teamarray to empty.
-
Disable Fetch Button
- Make the "Fetch" button disappear once the team has 6 Pokémon.
-
Disable Clear Button
- Prevent the "Clear" button from working until the team has 6 Pokémon.