sdy

update Timer in HomeDate.js

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