MHF Javascript/React Style Guide
React Components
Favor arrow functions for components
const MyComponent = () => {
...
}Using arrow functions avoids the
thiskeyword 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 MyComponentThough 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.
Component Naming
Use the filename as the component name.
Props Naming
Avoid using DOM component prop names for anything except the DOM attribute.
Wrong:
Correct:
Correct use of a DOM attribute
JSDOC
Use JsDoc to document your component and its props. This isn't mandatory, but it does help a lot.
Last updated
Was this helpful?