Skip to content

Instantly share code, notes, and snippets.

@johnborges
Last active September 6, 2019 18:15
Show Gist options
  • Save johnborges/a458d79fbe46c4fbd38cbfaf53f13c2e to your computer and use it in GitHub Desktop.
Save johnborges/a458d79fbe46c4fbd38cbfaf53f13c2e to your computer and use it in GitHub Desktop.
10 Component Commandments

10 React Component Commandments

  1. Document the Usage If you don't document how your component is supposed to be used, it's by definition useless.

  2. Allow for contextual semantics Allow your components to accept an as prop, which will consistently let you override what DOM element is being rendered.

function Grid({ as: Element, ...props }) {
  return <Element className="grid" {...props} />
}
Grid.defaultProps = {
  as: 'div',
};

function App() {
  return (
    <Grid as="main">
      <MoreContent />
    </Grid>
  );
}
  1. Avoid boolean props Boolean properties often doesn't scale with changing requirements. Instead, try to use enumerated values like strings for values that might have a chance to become anything other than a binary choice. Just save the boolean props for the truly binary choices.
<Button variant="primary" size="large">
  I am primarily a large button
</Button>
  1. Use props.children Much easier to use than having a content prop or something else that typically only accepts a simple value like text. props.children resembles how regular HTML works. Second, you're free to pass in whatever you want!

  2. Let the parent hook into internal logic

  3. Spread the remaining props

  4. Give sufficient defaults

  5. Don't rename HTML attributes

  6. Write prop types (or types)

  7. Design for the developers

summarized from this article

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