Last active
June 26, 2022 18:20
-
-
Save radekwarisch/35506bd533340d8b8c29849e92e54df4 to your computer and use it in GitHub Desktop.
React component example with comments marking decisions required from developer during creation process
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 * as React from 'react'; | |
import {navigate, trackEvent} from '@common'; | |
// decision 1: use style guide component or custom grid | |
import {Flex, Link} from 'brainly-style-guide'; | |
// decision 2: use Readonly in type or not | |
type PropsType = Readonly<{ | |
href: string; | |
}>; | |
type TrackEventTypes = 'linkClick'; | |
const LinkComponent = ({href}: PropsType) => { | |
// decision 3: memoize the handler or not | |
const onLinkClick = React.useCallback((event: React.SyntheticEvent) => { | |
// decision 4: use const for event label or rely on types | |
trackEvent<TrackEventTypes>('linkClick'); | |
event.preventDefault(); | |
// decision 5: use html nagivation or the one used below | |
navigate(href); | |
}, []); | |
return ( | |
<Flex direction="column"> | |
<Flex> | |
<Link href={href} onClick={onLinkClick}> | |
Redirect | |
</Link> | |
</Flex> | |
</Flex> | |
); | |
}; | |
// decision 6: memoize or not to memoize the component | |
export default React.memo<PropsType>(LinkComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment