Skip to content

Instantly share code, notes, and snippets.

@drublic
Last active September 7, 2019 08:05
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 drublic/e3c86a8f4850287884723d58f7ac981b to your computer and use it in GitHub Desktop.
Save drublic/e3c86a8f4850287884723d58f7ac981b to your computer and use it in GitHub Desktop.
Component JS to TS
// 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;
// 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