HomeDate.js
971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { useState, useEffect } from "react";
import styled from "styled-components";
import moment from "moment";
const HomeDate = styled.div`
display: flex;
flex-direction: column;
`;
const WeatherBox = styled.div``;
const WeatherSpan = styled.span``;
const DateBox = styled.div`
display: flex;
flex-direction: column;
`;
const DateSpan = styled.span`
font-size: ${(props) => {
if (props.className === "Clock") return "45px";
}};
`;
export default () => {
const [date, setDate] = useState(moment().format("h:mm:ss a"));
useEffect(() => {
let timer = setInterval(() => tick(), 1000);
return function cleanUp() {
clearInterval(timer);
};
});
function tick() {
setDate(moment().format("h:mm:ss a"));
}
return (
<HomeDate>
<WeatherBox>
<WeatherSpan /> Weather
</WeatherBox>
<DateBox>
<DateSpan className="Clock">{date}</DateSpan>
</DateBox>
</HomeDate>
);
};