- Practice the concepts
- React exercises
- Code along
- PROJECT: Hack Your Weather II
We'll start the week off with some online exercises again. Complete the exercises from the following:
Inside of your
React
fork, go to theweek2
folder and create a React application calledreact-exercises
(usecreate-react-app
). Inside of thesrc
folder create a JavaScript file with an appropriate name.
Exercise 1: New friend on demand
What if there was an app that could generate a new friend, instantly? How about we make one!
In this exercise we'll be using the following API endpoint: https://www.randomuser.me/api?results=1
- Create 3 functional components called
<Friend>
,<FriendProfile>
and<Button>
- Inside
<Friend>
create a state variable calledfriend
(with an initial state of{}
) and state handlersetFriend
- Inside
<Friend>
(before the return statement) also create a function calledgetFriend
. The purpose of this function is to make an API call tohttps://www.randomuser.me/api?results=1
. You can either usefetch
oraxios
. At the end of the function reassign thefriend
state variable usingsetFriend
- Inside the
<Button>
component, create a<button>
that has the text "Get a friend!" andonClick
attribute - Pass down the function
getFriend
to<Button>
- Inside
<FriendProfile>
create an<ul>
that showcases the following attributes of the person: The first and last names, the address and country, the email address, and finally also the phone number - Pass down the
friend
state variable to the<FriendProfile>
- Test if everything works by importing it into the top level component, which is
<App>
Exercise 2: Dog photo gallery
Let's make a randomized dog photo gallery!
In this exercise we'll be using the following API endpoint: https://dog.ceo/api/breeds/image/random
- Create 3 functional components:
<DogGallery>
,<DogPhoto>
and<Button>
- Inside
<DogGallery>
create a state variable calleddogPhotos
(initialize with value[]
) and state handlersetDogPhotos
- Inside (before the return statement) also create a function called
getDogPhoto
. The purpose of this function is to make an API call tohttps://dog.ceo/api/breeds/image/random
. You can either usefetch
oraxios
. At the end of the function push the newdog image URL
into thedogPhotos
array state variable usingsetDogPhotos
- Inside the
<Button>
component, create a<button>
that has the text "Get a dog!" andonClick
attribute - Pass down the function
getDogPhoto
to<Button>
- Inside
<DogGallery>
return all the dogs stored in thedogPhotos
array using themap()
function. Pass down eachdogPhoto
into an instance of<DogPhoto>
. - However, when there are no dogs in the array yet make sure to display the message "Get your first dog by clicking the button!"
- Test your components by importing it into the top level component, which is
<App>
Exercise 3: Random Joke Generator
Never run out of jokes with the Random Joke Generator!
In this exercise we'll be using the following API endpoint: https://official-joke-api.appspot.com/random_joke
- Create 2 functional components:
<RandomJoke>
and<Joke>
- Inside
<RandomJoke>
create a state variable calledjoke
(with initial state{}
) and state handlersetJoke
- Make use of the
useEffect
hook to make an asynchronous API call to the API endpoint. Put the end result into the state variable usingsetJoke
- Now pass the reassigned state variable
joke
to the<Joke>
component - Inside the
<Joke>
component, display thesetup
andpunchline
attributes in 2<p>
tags - Test out your work by importing your components into the top level component, which is
<App>
Create a new GitHub repository for this project. It's a portfolio piece!
In the following tutorial you'll learn how to build an Expense Tracker
. You'll do so using React Hooks, but also using the Context API! The later you'll be learning about more formally in week 4, but best to get familiar as soon as possible.
Happy learning!
Make sure you're building on last week's codebase.
Let's continue building on our application! This week you'll replace the application's data source: instead of using a JSON file you'll be using an external API, called OpenWeatherMap. In addition to that, you'll add a search functionality
that allows a user to type in a city name and retrieve weather data on it!
- Sign up for an API key at OpenWeatherMap App ID
- Create a
.env
file in the root of your project folder - Paste the API key inside the
.env
file with the nameREACT_APP_OPENWEATHERMAP_API_KEY
By the end of this week's assignment your application should look similar to this:
Here are the requirements your project needs to fulfill:
- Your form should have
controlled inputs
- Make an API call when the button is clicked
- Display the city + weather information similar to week 1
- Make a component for the search
- Incorporate error handling for the API call
- Show user feedback when there are no cities searched for yet
- Redeploy your static site
Hints:
- For the form, use the
onChange
andvalue
attributes - Take the name of the city to use in the API call
After you've finished your list it's time to show us what you got! Upload all your files to a forked repository (a copy from the original, which in this case is the React repository) using GIT. Then make a pull request to your teacher's forked repository.
If you need a refresher, take a look at the following guide to see how it's done.
The homework that needs to be submitted is the following:
- React exercises
- Project: Hack Your Weather II
Deadline Saturday 23.59 CET