npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-forecast-query

v0.2.1

Published

Higher order React component delivering weather information from open weather api. Wrapper for forecast-query package.

Downloads

29

Readme

react-forecast-query

A simple higher order component capable of injecting weather information from open weather api into a component

Example of use

<div>
    <input value={state.value} onInput={e => this.setState({ value: (e.target as HTMLInputElement).value })} />
    <button onClick={() => this.setState(prevState => ({ apiKey: prevState.value }))}>Test component</button>
    {state.apiKey && (
        <WeatherDisplay 
            apiKey={state.apiKey}
            label="Cloudy weather measurements"
            query={['clouds', 'cloudy']}
            by="hour"
            loadingComponent={() => <div>Loading...</div>}
            setup={forecast => forecast
                .at(today, fourDaysFromNow)
                .around(50.08804, 14.42076)
                .units('metric')
                .language('cz')}
        />
    )}
</div>

Example of how to define injected component


import React, { FunctionComponent } from "react";
import { InjectedForecastResults } from "react-forecast-query";
import weatherEnhancer from "react-forecast-query";

export interface WeatherDisplayProps {
    label: string;
}

const WeatherDisplay: FunctionComponent<WeatherDisplayProps & InjectedForecastResults> = props => (
    <section>
        <label>{props.label}</label>
        <ul>
            {props.data.clouds?.map(clouds => (
                <li>
                    {clouds.date.toDateString()} {clouds.date.toTimeString()} - {clouds.value}
                </li>
            ))}
        </ul>
        <ul>
            {props.data.cloudy?.map(weather => (
                <li>
                    <img src={props.forecast.icon(weather.icon)} />
                    <div>
                        {weather.description}
                    </div>
                </li>
            ))}
        </ul>
    </section>
);

export default weatherEnhancer(WeatherDisplay);

Use of the component

Use setup props to add location, units, language and dates, then define a string list of information you want to receive.

List of information is derived from ".list()" function in forecast-query.

"By" parameter refers to how descriptive the list should be ("hour" delivers hourly weather updates, "day" daily ones).

"loadingComponent" will display whatever component you want when the api is loading information.

Changes since 0.1.0

Added error component

Changes since 0.1.1

Fix error component error reporting

Changes since 0.2.0

Add support for gps geolocation in browser.

Example of use


<WeatherDisplay 
    apiKey={state.apiKey}
    label="Cloudy weather measurements"
    query={['clouds', 'cloudy']}
    by="hour"
    loadingComponent={() => <div>Loading...</div>}
    errorComponent={props => {
        console.error(props.error);
        return <div>Error!</div>
    }}
    geo // Try and get geolocation
    updateGeo={1} // Updates every minute
    setup={forecast => forecast
        .at(today, fourDaysFromNow)
        .units('metric') // No location setup needed
        .language('cz')}
/>

Changes since 0.2.1

Fixed a bug in typing of enhancer concerning Component class.

More information

For more information about weather prediction from open weather api please visit: https://www.npmjs.com/package/forecast-query

If you find any bug or have an idea of how to improve this package, please file in an issue or a pull request