Skip to content

Instantly share code, notes, and snippets.

@mohanramphp
Created January 9, 2019 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohanramphp/2df0063cd5619ab882f5bea869c23c11 to your computer and use it in GitHub Desktop.
Save mohanramphp/2df0063cd5619ab882f5bea869c23c11 to your computer and use it in GitHub Desktop.
Watch Component using React Hook
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