GithubHelp home page GithubHelp logo

fetch-weather's Introduction

Fetch the Weather

Let's use our new-found react skills to create a weather app!

You will need:

  • Fetch (built-in to JavaScript) or axios (3rd party, remember to npm install axios)
  • Create forms that store data with local state

Implement the Fetch API

Resources:

Time to show off! You're going to display the weather on your app.

You'll use the OpenWeather API to fetch weather information from a certain zip code and update your blog to display the current weather.

Solution

At the end of this exercise, your solution will look something like what's shown here.


Requirements

Make a new component called Weather.

On your Weather page, ask the user to input a zip code.

  • You can learn more about forms here.

    • When this event fires, take the event.target.value and fetch() from the OpenWeather API.
  • Use the response from the API to display the current temperature, the high and low temperatures, the current weather description, and the name of the city.

    • Note: Our solution uses Fahrenheit. You're free to use Celsius or Kelvins if you'd like.

Important Notes:

  • Because the OpenWeather API is not an open API, every request must end with this API key: &appid=052f26926ae9784c2d677ca7bc5dec98.

  • As an example, this is a URL to which you might send a fetch() request: http://api.openweathermap.org/data/2.5/weather?zip=60614,us&appid=052f26926ae9784c2d677ca7bc5dec98.

Skeleton Code (Starter)

Here is a functional Weather component starter:

import { Component } from 'react';

class Weather extends Component {
  state = {
    // Your required states here
  }

  handleChange = (event) => {
    this.setState({ zipcode: event.target.value }, () => {
      console.log('Your zip code is', this.state.zipcode);
    });
  }

  handleSubmit = (event) => {
    // Your fetch call here
    // Your state updates go under function(json)
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <label htmlFor="zipcode">Please enter your zip code for the weather:</label>
          <input 
            id="zipcode" 
            type="text" 
            onChange={this.handleChange} 
          />
          <input type="submit" value="Get my forecast!" />
        </form>
        <div>
          { /* Display weather information here */}
        </div>
      </div>
    )
  }
}


export default Weather;

Need a Hint or Two?

Hint 1
You'll only need to create and implement the `Weather` component.
Hint 2
The `value` of your text fields need to be 'controlled' in your components state
Hint 3
The temperature reading comes in units of Kelvin by default - check the API for the `units` parameter to get the reading in Farenheit or Celcius

Bonus

Here are some extra ideas to challenge yourself if you have time:

  • Icons make every weather app come to life! There's a good way to do this - hunt around in the returned JSON for a recommended icon.
  • Spend a little time styling the page. Try to get it close to the example image above!
  • Make your current weather into a 5-day forecast
  • Make a Use current location button so the user can click that instead of providing the location.

fetch-weather's People

Contributors

h64 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.