Created
January 9, 2019 09:03
-
-
Save mohanramphp/2df0063cd5619ab882f5bea869c23c11 to your computer and use it in GitHub Desktop.
Watch Component using React Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import PropTypes from 'prop-types'; | |
import moment from 'moment'; | |
import { Strap, Bezel, Screen, Face } from './WatchStyledComponents'; | |
import { DefaultFace } from './WatchFaceComponents'; | |
/** | |
* custom Hooks | |
* @param {*} d | |
*/ | |
const useDate = (d) => { | |
const [date, setDate] = useState(d); | |
useEffect(() => { | |
console.log('useEffect called'); | |
const id = setInterval(() => setDate(moment()), 1000); | |
return () => { | |
console.log('useEffect flush called'); | |
clearInterval(id); | |
} | |
}, []); | |
return date; | |
} | |
export const Watch = ({ face = date => <DefaultFace date={date} /> }) => { | |
const date = useDate(moment()); | |
return ( | |
<Strap> | |
<Bezel> | |
<Screen> | |
<Face>{face(date)}</Face> | |
</Screen> | |
</Bezel> | |
</Strap> | |
); | |
}; | |
Watch.propTypes = { face: PropTypes.func }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment