Naming

  • Extensions: Use .jsx extension for React components.
  • Filename: Use PascalCase for filenames. E.g., ReservationCard.jsx.
  • Reference Naming: Use PascalCase for React components and camelCase for their instances. eslint: react/jsx-pascal-case

    1. // bad
    2. import reservationCard from './ReservationCard';
    3. // good
    4. import ReservationCard from './ReservationCard';
    5. // bad
    6. const ReservationItem = <ReservationCard />;
    7. // good
    8. const reservationItem = <ReservationCard />;
  • Component Naming: Use the filename as the component name. For example, ReservationCard.jsx should have a reference name of ReservationCard. However, for root components of a directory, use index.jsx as the filename and use the directory name as the component name:

    1. // bad
    2. import Footer from './Footer/Footer';
    3. // bad
    4. import Footer from './Footer/index';
    5. // good
    6. import Footer from './Footer';
  • Higher-order Component Naming: Use a composite of the higher-order component’s name and the passed-in component’s name as the displayName on the generated component. For example, the higher-order component withFoo(), when passed a component Bar should produce a component with a displayName of withFoo(Bar).

    Why? A component’s displayName may be used by developer tools or in error messages, and having a value that clearly expresses this relationship helps people understand what is happening.

    1. // bad
    2. export default function withFoo(WrappedComponent) {
    3. return function WithFoo(props) {
    4. return <WrappedComponent {...props} foo />;
    5. }
    6. }
    7. // good
    8. export default function withFoo(WrappedComponent) {
    9. function WithFoo(props) {
    10. return <WrappedComponent {...props} foo />;
    11. }
    12. const wrappedComponentName = WrappedComponent.displayName
    13. || WrappedComponent.name
    14. || 'Component';
    15. WithFoo.displayName = `withFoo(${wrappedComponentName})`;
    16. return WithFoo;
    17. }
  • Props Naming: Avoid using DOM component prop names for different purposes.

    Why? People expect props like style and className to mean one specific thing. Varying this API for a subset of your app makes the code less readable and less maintainable, and may cause bugs.

    1. // bad
    2. <MyComponent style="fancy" />
    3. // bad
    4. <MyComponent className="fancy" />
    5. // good
    6. <MyComponent variant="fancy" />