Skip to content

Instantly share code, notes, and snippets.

@richardscarrott
Last active March 8, 2018 13:36
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 richardscarrott/712425ff5565eaf74fa88e8a28e660c0 to your computer and use it in GitHub Desktop.
Save richardscarrott/712425ff5565eaf74fa88e8a28e660c0 to your computer and use it in GitHub Desktop.
React Component Design
const Button = ({ tagName: TagName = 'button', ...rest }) => <TagName className="button" {...rest} />;
const App = () => {
return (
<>
<Button onClick={() => console.log('clicked')}>renders HTMLButtonElement (default)</Button>
<Button tagName="a" href="http://google.com">renders HTMLAnchorElement (for functional purposes)</Button>
<Button tagName="h3">renders HTMLHeadingElement (for weird SEO purposes)</Button>
</>
);
}
@richardscarrott
Copy link
Author

It's key to understand that a Button component is a Button component purely because it looks a certain way, not because of the markup it renders. i.e. if it looks different, use a different component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment