Last active
September 7, 2019 08:05
-
-
Save drublic/e3c86a8f4850287884723d58f7ac981b to your computer and use it in GitHub Desktop.
Component JS to TS
This file contains hidden or 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
// This is an exisiting React Component written as a Function Component using JavaScript. | |
import React from "react"; | |
import { string, any } from "prop-types"; | |
const Card = ({ | |
headline, | |
createdAt, | |
children, | |
}) => { | |
const classes = useStyles(); | |
return ( | |
<div className={classes.root}> | |
<h2 classname={classes.header}>{headline}</h2> | |
<p>Created at: {createdAt}</p> | |
{children} | |
</div> | |
); | |
}; | |
Card.propTypes = { | |
headline: string.isRequired, | |
createdAt: string.isRequired, | |
children: any, | |
}; | |
export default Card; |
This file contains hidden or 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
// Here is the component migrated to TypeScript using the FunctionComponent | |
// generic to use an interface for the props of the component which helps | |
// to unserstand the API of the component better and with autocomplete features. | |
import React, { FunctionComponent } from "react"; | |
interface Props { | |
headline: string; | |
createdAt: string; | |
children: any; | |
}; | |
const Card: FunctionComponent<Props> = ({ | |
headline, | |
createdAt, | |
children, | |
}) => { | |
const classes = useStyles(); | |
return ( | |
<div className={classes.root}> | |
<h2 classname={classes.header}>{headline}</h2> | |
<p>Created at: {createdAt}</p> | |
{children} | |
</div> | |
); | |
}; | |
export default Card; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment