【发布时间】:2021-03-26 20:51:19
【问题描述】:
我想做一个天气应用。但是字符串没有出现在我想要的组件上。
innerText 在 React 中如何使用?
这是我的 Weather.jsx
import React from "react";
const Weather = () => {
const API_KEY = '123456789';
const COORDS = "coords";
const weather = document.getElementById("weather");
const getWeather = (lat, lng) => {
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${API_KEY}&units=metric`
)
.then(function (response) {
return response.json();
})
.then(function (json) {
const temperature = json.main.temp;
const place = json.name;
weather.innerText = `${temperature}℃,\n ${place}`;
});
};
};
export default Weather;
这是显示天气的屏幕代码。
import React from "react";
import styled from "styled-components";
import Weather from "../../Components/Weather";
const DetailPresenter = () => {
return (
<>
<DetailContainer>
<div id="weather">{Weather()}</div>
</DetailContainer>
</>
);
};
export default DetailPresenter;
【问题讨论】:
-
如果一定要引用元素,可以看this post,否则我会选择@Drew Reese提供的解决方案
标签: reactjs react-hooks getelementbyid weather innertext