✨
Help-a-Family Manual
  • Help a Family Docs
  • Info for Charities
  • Info for API Users
  • Tutorial
  • Contentful
  • Environment Variables
    • Lighthouse CI
  • Troubleshooting
  • Contributing to Help-a-Family
    • Fork and Clone the Repo
    • MHF Javascript/React Style Guide
    • MHF CSS Guide
    • Contributor Power Tips
Powered by GitBook
On this page
  • React Components
  • Favor arrow functions for components
  • Destructure Props
  • Exports
  • Component Naming
  • Props Naming
  • JSDOC

Was this helpful?

  1. Contributing to Help-a-Family

MHF Javascript/React Style Guide

React Components

Favor arrow functions for components

const MyComponent = () => {
  ...
}

Using arrow functions avoids the this keyword behavior nuances

Destructure Props

(props) should be ({propName1, propName2})

This gives clarity to the codebase in your component's use of props

Exports

Favor export default for components and make one component per file as opposed to named export with multiple components.

const MyComponent = () => {
  ...
}

export default MyComponent

Though Typescript recommends this practice for code readability, there is no hard reasoning for this rule except for consistency.

If your component seems better suited with named exports feel free to do so.

Note: Utility and Constants Files

named exports are better for these files.

export function myHelper(){
  ...
}

Component Naming

Use the filename as the component name.

const MyComponent = () => {...}
// should be 
MyComponent.js

Props Naming

Avoid using DOM component prop names for anything except the DOM attribute.

Wrong:

<MyComponent onClick={handleClick} />
...
const MyComponent = ({onClick}) => {
  ...
  setIsEditing(!onClick)
  ...
}

Correct:

<MyComponent isEditing={handleIsEditing} />
...
const MyComponent = ({isEditing}) => {
  ...
  setIsEditing(!isEditing)
  ...
}

Correct use of a DOM attribute

<MyComponent onClick={handleClick} />
...
const MyComponent = ({onClick}) => {
  return <button onClick={onClick}>
}

JSDOC

Use JsDoc to document your component and its props. This isn't mandatory, but it does help a lot.

/**
* MyComponent does x, y, z
*
* @param {Object} props - { propOne }
* @param {String} props.propOne - propOne does x, y, z
*
*/
const MyComponent = ({propOne}) => {...}

PreviousFork and Clone the RepoNextMHF CSS Guide

Last updated 3 years ago

Was this helpful?