Components
This section is about how we build React components at Vetspire. It includes some practices to follow and tips to ensure we are all building consistent components.
General considerations for building components
New components MUST be functional and not class components. This allows us to build modern components that use the latest technologies such as hooks.
Components MUST be named exported, ideally at the declaration level. This makes for a more consistent & readable codebase. It also enforces better naming standards.
Components MUST be named imported.
We SHOULD follow all the standards defined in the React docs when building components.
Component File Structure
All new shared components MUST be built in the
/web/shared-components
directory.
The recommended approach to building components is as follows:
In the above example index.tsx
would contain the main export export const ExampleComponent...
.
For modules with import { ExampleComponent} from "/path/to/ExampleComponent"
, we favour building focused, small components to contain all related types, styles, and non-shared utility functions within the same file. Utility modules and functions are placed in a utils
folder.
Component Structure
As noted in the "Styling Components" section, styles SHOULD be extracted into
const
variables/styled components.Component files that contain multiple components MUST contain the main component at the top of the file.
Components MUST have minimal logic in the return/render block. Any logic must be moved outside the component or into the component body
Example of the above:
Correct (as logic is minimal) โ
Correct (as logic is complicated) โ
Incorrect (as logic is complicated and so should be pulled out of the return block)โ
We MUST pull complicated logic out of render prop block.
Incorrectโ
Correct โ
Component sections MUST follow this order:
Imports
Type definition for main component
Main component itself
Helper functions for main component (or those shared across components in the file)
Extracted styles for main component (or those shared across components in the file)
Subcomponent type
Subcomponent itself
Helper functions for subcomponent
Extracted styles for subcomponent
Non-comprehensive example below to demonstrate:
Imports
We MUST use absolute imports.
Correct โ
import something from "path/to/something"
Incorrect โ
import something from "../../something"
We MUST use named imports/exports (unless in the rare case where it isn't impossible)
Correct โ
import { something } from "path/to/something"
Incorrect โ
import something from "path/to/something"
We MUST alphabetise imports in the following order
external
,internal
,current_directory
.
Correct โ
We MUST avoid circular imports when possible.
Styling Components
We MUST use composable styles using
emotion.
We MUST avoid using the legacy
styled-component
package.Any inlined style that consist of three or more rules MUST be defined in a
const
variable for readability and reusability.
Correct โ
Incorrect โ
We MUST use the
defaultValue
value for styling.We MUST use colors from theme file.
CSS rules MUST appear in alphabetical order.
Last updated