forked from CodeYourFuture/cyf-weather
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Main.js #19
Closed
Closed
Create Main.js #19
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f0709d8
Create Main.js
zkhing 26040ad
moving Zaw's comments to app.js
SallyMcGrath 0718549
deleting main component as we think we don't need this complexity
SallyMcGrath 08fa292
Adding weather image
zkhing dac08f8
Fetching API
zkhing 87d8e92
Button onclick
zkhing a18dd57
Setting temp
zkhing be36012
Fetch temp from API
zkhing 85d523f
Matching up the weather icon images
zkhing ebb724f
Merge branch 'main' into main
LucyMac accfe59
Future weather and refactor code
zkhing 544ee29
Merge branch 'main' of https://github.com/zkhing/RainyPlaytime into f…
zkhing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
@use "theme/utilities.scss"; | ||
@use "theme/global.scss"; | ||
|
||
|
||
.c-site-main{ | ||
background-color: skyblue; | ||
} | ||
|
||
.weather-img{ | ||
width: 200px; | ||
height: 200px; | ||
// max-width: 100%; | ||
object-fit: cover; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
import React from "react"; | ||
import './Header.scss' | ||
import "./Header.scss"; | ||
import React, { useState, useEffect } from "react"; | ||
|
||
const Header = ({title}) => | ||
const Header = ({ title }) => { | ||
const [data, setData] = useState([]); | ||
const [city, setCity] = useState(""); | ||
|
||
function changeCity(){ | ||
setData(data); | ||
} | ||
|
||
useEffect(() => { | ||
fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?q=liverpool&units=metric&appid=3b86046cce0de3be7cfa8369f4540b37` | ||
) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setData(data); | ||
}) | ||
.catch((e) => console.log(e.message)); | ||
}, []); | ||
|
||
return ( | ||
<header className="c-site-header"> | ||
<h1 className="c-site-header__title">{title}</h1> | ||
</header> | ||
<h1 className="c-site-header__title">{title}</h1> | ||
|
||
<input | ||
className="city" | ||
type="text" | ||
value={city} | ||
onChange={(event) => setCity(event.target.value)} | ||
/> | ||
|
||
<button onClick={changeCity}>Search</button> | ||
|
||
<div> | ||
<h2>Temp:{data?.main?.temp?.toFixed()}°C</h2> | ||
</div> | ||
|
||
{/* {data.main.temp ? <h1>Temp:{data.main.temp}°C </h1> : null} */} | ||
</header> | ||
); | ||
}; | ||
|
||
export default Header; | ||
export default Header; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better but you are still saving the data in 2 different places (lines 9 and 18). You only need one.
I think what you need to do is:
onChange
in the text input, save the user's input, just like you've done - don't change anything here it's perfect.getNewWeather
function?${city}
so the API call changes.If you do all this correctly it should update the temperature for each new city (it works for me 😊)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(If you're wondering when to use
useEffect
, it would be perfect if you didn't have a 'search' button. For example if you just had the text input but no button, then you would want to make a new API call each time the user types a new letter. In this case, you would still save 'city'onChange
but you would addcity
as a dependency in the useEffect array, and the API call inside the useEffect would get triggered each time the value ofcity
changes.This is very inefficient because you would make too many calls to the API, so in this case it's best to move the API call to be triggered by a button click instead.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much Lucy! It works now. I didn't realise that fetch API works without useEffect. :) Now I learned when to use useEffect.