zetabug / react-weather-application Goto Github PK
View Code? Open in Web Editor NEWThis is a very simple React Js based weather application, that uses Weather API.
Home Page: https://stellar-pie-6964b6.netlify.app
This is a very simple React Js based weather application, that uses Weather API.
Home Page: https://stellar-pie-6964b6.netlify.app
App component is currently bloated and using old JS syntax
const App = () => {
const [data, setData] = useState(null);
const [place, setPlace] = useState(null);
const [search, setSearch] = useState("");
const [location, setLocation] = useState("");
const [isLoading, setIsLoading] = useState(false);
const geoHandler = async () => {
setSearch("");
setIsLoading(true);
await navigator.geolocation.getCurrentPosition((position) => {
const crd = position.coords;
const latitude = crd.latitude;
const longitude = crd.longitude;
setLocation(`${latitude},${longitude}`);
});
};
const fetchData = useCallback(async (search, location) => {
const options = {
method: "GET",
headers: {
"X-RapidAPI-Key": "0173291af0msh62b3ca25953f210p13d732jsn66b4d9f97708",
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com",
},
};
const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${
search || location
}`;
const response = await fetch(url, options);
const responseData = await response.json();
return responseData;
}, []);
useEffect(() => {
fetchData(search, location).then((responseData) => {
setPlace(responseData.location);
setData(responseData.current);
setIsLoading(false);
});
}, [location, search, fetchData]);
return (
<>
<div className="container">
<h1>WEATHER APPLICATION</h1>
<div className="search-sec">
<input
type="text"
value={search}
placeholder="Search by City..."
onChange={(e) => setSearch(e.target.value)}
/>
</div>
<div>
<p>or</p>
</div>
<div>
<button onClick={geoHandler}>Find me!</button>
</div>
<br />
<br />
{isLoading && <LoadingIndicator />}
{data ? <Weather place={place} data={data} /> : <p>no data found 😬</p>}
</div>
<span className="credit">Ranvir@zetabug/github</span>
</>
);
};
export default App;
const Weather = ({ place, data }) => {
return (
<div className="output-sec">
<div className="location">
{place.name}, {place.region}
</div>
<img src={data.condition.icon} alt="" />
<div className="sky-status">{data.condition.text}</div>
<div className="temp">Temperature : {data.temp_c}°C</div>
<div className="humidity">Humidity : {data.humidity}</div>
</div>
);
};
If interested, review and merge PR
Thank you =)
hi, @zetabug 👋
I want to add dark mode to the website. Can I start working on that?
The RapidAPI key is exposed in the code, which means that anyone can take it and do a large amount of requests, surpassing the free plan limit and causing financial problems to the owner of the key.
In development (local): create an environment variable (ex: RAPID_API_KEY), and assign your RapidAPI key as its value. Make sure the .env file is listed on .gitignore so the key don't gets exposed in the public repository. Learn more about environment variables in React on this article.
In production (Netlify): create an environment variable (learn more in the Netlify documentation) with the same name and value as the local environment variable.
This is not the ideal solution, because the key will be still present in the request and anyone will be able to see it on the DevTools network tab. The ideal solution would be create a backend to access the API with that key. But at least the provided solution is a best practice and removes your key from GitHub.
E-mail: [email protected]
Readme still has CRA Template
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.