import React, { useEffect, useState } from 'react'; import { IOTWeatherProps } from './interface'; import './style/index.less'; const prefixCls = 'iot-weather-out'; type items = { flag: boolean, text: string } const IOTWeather: React.FC = ({ weather, ...rest }) => { const [location, setLocation] = useState({ flag: false, text: '' }); const [temperature, setTemperature] = useState({ flag: false, text: '' }); const [humidity, setHumidity] = useState({ flag: false, text: '' }); const [pm25, setPm25] = useState({ flag: false, text: '' }); useEffect(() => { if (weather) { setLocation({ ...weather['location'] }); setTemperature({ ...weather['temperature'] }); setHumidity({ ...weather['humidity'] }); setPm25({ ...weather['pm25'] }); } }, [weather]) return (
{ location.flag && ( <> {location.text ? location.text : '定位失败'} )} { temperature.flag && ( <> 温度 {temperature.text ? temperature.text + '℃' : '--'} )} {humidity.flag && ( <> 湿度 {humidity.text ? humidity.text : '--'} )} {pm25.flag && ( <> PM2.5 {pm25.text ? pm25.text : '--'} )}
{/* { location.flag && (
{location.text ? location.text : '定位失败'}
) } { temperature.flag && (
温度 {temperature.text ? temperature.text : '--'}
) } { humidity.flag && (
湿度 {humidity.text ? humidity.text : '--'}
) } { pm25.flag && (
PM2.5 {pm25.text ? pm25.text : '---'}
) }
占
*/}
); }; export default IOTWeather;