teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

変更

2021/01/30 13:17

投稿

suzukitoshinari
suzukitoshinari

スコア6

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > Error: Objects are not valid as a React child (found: object with keys {coord, weather, base, main, visibility, wind, clouds, dt, sys, timezone, id, name, cod}). If you meant to render a collection of children, use an array instead.
4
4
 
5
- 該当箇所
5
+ 該当エラー箇所
6
6
  > src/Weather.js:59
7
7
  56 | useEffect(() => {
8
8
  57 | // const key = 'be690936e66de4f05bda2ccf9f534c2c';

1

内容の変更

2021/01/30 13:17

投稿

suzukitoshinari
suzukitoshinari

スコア6

title CHANGED
File without changes
body CHANGED
@@ -54,4 +54,84 @@
54
54
  "last 1 safari version"
55
55
  ]
56
56
  }
57
- }
57
+ }
58
+
59
+
60
+ 追加箇所
61
+
62
+ > xhr.js:177 GET http://api.openweathermap.org/data/2.5/weather?q=city&appid=%22be690936e66de4f05bda2ccf9f534c2c%22 401 (Unauthorized)
63
+
64
+ > createError.js:16 Uncaught (in promise) Error: Request failed with status code 401
65
+ at createError (createError.js:16)
66
+ at settle (settle.js:17)
67
+ at XMLHttpRequest.handleLoad (xhr.js:62)
68
+
69
+ コード
70
+
71
+ ```
72
+ import { Button, Paper, TextField } from "@material-ui/core";
73
+ import React, { useState, useEffect } from "react";
74
+ import SendIcon from "@material-ui/icons/Send";
75
+ import axios from "axios";
76
+
77
+ const Weather = () => {
78
+ const [value, setValue] = useState("");
79
+ const [temp, settemp] = useState("");
80
+ const [typecity, settypecity] = useState("");
81
+ const [city, setcity] = useState("Delhi");
82
+ const [img, setimg] = useState("");
83
+ const [disc, setdisc] = useState("");
84
+
85
+ const citySelect = (e) => {
86
+ e.preventDefault();
87
+ setcity(typecity);
88
+ };
89
+ useEffect(() => {
90
+ axios.get(
91
+ `http://api.openweathermap.org/data/2.5/weather?q=city&appid="自身のAPI"`
92
+ ).then((res) => {
93
+ setValue(res.data);
94
+ settemp(res.data.main);
95
+ setimg(
96
+ `http://openweathermap.org/img/wn/res.data.weather[0].icon.png`
97
+ );
98
+ setdisc(res.data.weather[0].description);
99
+ });
100
+ }, [city]);
101
+
102
+ return (
103
+ <div>
104
+ <Paper className="paper">
105
+ <form onSubmit={citySelect} className="elementcenter">
106
+ <TextField
107
+ placeholder="type city here"
108
+ value={typecity}
109
+ onChange={(e) => settypecity(e.target.value)}
110
+ />
111
+ <Button type="submit">
112
+ <SendIcon />
113
+ </Button>
114
+ </form>
115
+ <h6 className="fontcss">{value.name}</h6>
116
+ <img src={img} alt="weather icon" className="imgcss" />
117
+ <h6 className="fontcss">{disc}</h6>
118
+ <div className="elementcenter">
119
+ <p>
120
+ Min <br />
121
+ {`${Math.floor(temp.temp_min - 273.15)}° C`}
122
+ </p>
123
+ <h6 className="fontcss">{`${Math.floor(temp.temp - 273.15)}° C`}</h6>
124
+ <p>
125
+ Min <br />
126
+ {`${Math.floor(temp.temp_max - 273.15)}° C`}
127
+ </p>
128
+ </div>
129
+ </Paper>
130
+ </div>
131
+ );
132
+ };
133
+
134
+ export default Weather;
135
+
136
+
137
+ ```