-
Notifications
You must be signed in to change notification settings - Fork 8
Description
While we mostly use kebab case for file names with no jsx extension. The proposed guidelines by AirBnb are very compelling. What are everyone else's thoughts? Below is the except from: https://github.com/airbnb/javascript/tree/master/react#naming
Naming
-
Extensions: Use
.jsxextension 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// bad import reservationCard from './ReservationCard'; // good import ReservationCard from './ReservationCard'; // bad const ReservationItem = <ReservationCard />; // good const reservationItem = <ReservationCard />;
-
Component Naming: Use the filename as the component name. For example,
ReservationCard.jsxshould have a reference name ofReservationCard. However, for root components of a directory, useindex.jsxas the filename and use the directory name as the component name:// bad import Footer from './Footer/Footer'; // bad import Footer from './Footer/index'; // good 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
displayNameon the generated component. For example, the higher-order componentwithFoo(), when passed a componentBarshould produce a component with adisplayNameofwithFoo(Bar).Why? A component's
displayNamemay be used by developer tools or in error messages, and having a value that clearly expresses this relationship helps people understand what is happening.// bad export default function withFoo(WrappedComponent) { return function WithFoo(props) { return <WrappedComponent {...props} foo />; } } // good export default function withFoo(WrappedComponent) { function WithFoo(props) { return <WrappedComponent {...props} foo />; } const wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || 'Component'; WithFoo.displayName = `withFoo(${wrappedComponentName})`; return WithFoo; }
-
Props Naming: Avoid using DOM component prop names for different purposes.
Why? People expect props like
styleandclassNameto 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.// bad <MyComponent style="fancy" /> // bad <MyComponent className="fancy" /> // good <MyComponent variant="fancy" />