react-http-request: This is a React component that exposes network request functionality. It can be used to perform a network request and parse the response
http-react: This is a React hooks library for data fetching. It's built on top of the native Fetch API and can be used to make the HTTP request to an API server, in this case: the OpenWeatherMap API
Formik: This is a popular open-source library for managing forms in React applications. It can be used to create the input form for specifying the city
Please note that the exact packages you need may vary depending on the specific requirements of your application and your personal preferences.
Always refer to the official documentation of each package for the most accurate and up-to-date information.
Welcome to the Weather App Lab!
Today, we will be building a simple weather application using React.js and the OpenWeatherMap API. This lab is designed to be completed within a 2-hour class duration. {Quite similar to the expectations of Lab Test 2}.
## Prerequisites
Before we begin, you will need to sign up for a free account with OpenWeatherMap to get an API key. You can get an API key by following this link:
Cloud-based IDEs offer several advantages over traditional local development environments. They offer hassle-free configuration, as the development environment runs fully on a browser, eliminating the need for complex setup processes.
They also allow for easy collaboration and are accessible from any device that supports a modern web browser. For this lab, we will be using CodePen, a free cloud-based IDE[3][11].
## Step-by-Step Guide
### Step 1: Setting Up the Project on CodePen.io
1. Navigate to https://codepen.io/ and sign up for a free account if you don't have one.
2. Click on the "Create" button at the top right corner and select "New Pen".
3. You should now see three sections: HTML, CSS, and JS. We will be writing our code in these sections.
### Step 2: Writing the HTML
In the HTML section, write the following code:
```html
<div id="app"></div>
```
This is the root element where our React application will be mounted[12].
### Step 3: Adding React Libraries
In the JS section, click on the settings icon (gear icon) and under "JavaScript Preprocessor", select "Babel". This will allow us to write modern JavaScript that is compatible with older browsers.
Next, under "Add External Scripts/Pens", add the following two URLs to include the React and ReactDOM libraries:
Remember to replace `YOUR_API_KEY` with your actual API key from OpenWeatherMap[14].
To allow the user to specify the city to pull the weather from, we can add an input form to our React.js application. This form will give the user the option to enter a city name. When the user submits the form, the application will make an HTTP call to the OpenWeatherMap API to retrieve the weather data for the specified city. Here's how you can implement this:
### Step 6: Adding User Input
First, we need to add an input form to our `WeatherApp` component. Modify the `render` method as follows:
Remember to replace `YOUR_API_KEY` with your actual API key from OpenWeatherMap[5].
Now, when the user enters a city name into the input form and clicks the "Get Weather" button, the application will fetch and display the weather data for the specified city.
In the provided lab, the output is presented on the app screen by rendering the fetched weather data as JSX elements within the WeatherApp component. The data is fetched from the OpenWeatherMap API as a JSON object, which is then unpacked and used to update the component's state
When the component's state is updated with the fetched weather data, the render method of the WeatherApp component is called. This method returns JSX elements that display the weather information, such as the city name, temperature, and weather description. The JSX elements are created using the data from the JSON object stored in the component's state
The networking with the OpenWeatherMap API server is done using the fetch function, which is a built-in JavaScript function for making HTTP requests. In the WeatherApp component, the fetch function is called with the API URL, which includes the city name and the API key. The response from the API server is then converted to a JSON object using the response.json() method. Once the JSON object is obtained, it is used to update the component's state, triggering a re-render and displaying the fetched weather data on the screen
Congratulations! You have just built a simple weather application using React.js and the OpenWeatherMap API. This lab session has demonstrated the power of React.js for building interactive web applications, and the convenience of using a cloud-based IDE for development. Happy coding!