← Back: Documentation Overview
How to write react components in Samfundet4
.
This document will show examples of desired component-structure, followed by a detailed list of conventions we aim to use as default when creating new components.
It's not expected to always keep every component up-to-date, this is simply a baseline. Some components may not fit these conventions.
Star (
*
) annotates a folder.
Dash (-
) annotates a file.
* ...
* Components
* SomeGenericComponent
- index.ts
- SomeGenericComponent.scss
- SomeGenericComponent.stories.scss
- SomeGenericComponent.tsx
- utils.tsx
* ...
* ...
* Pages
* SomePage
* components
- index.ts
* SomeSub
* utils
- index.ts
- navigation.ts
- namespacing.ts
- index.ts
- SomePage.scss
- SomePage.tsx
- testdata.ts
* ...
Component written for reusability. These components are typically included in the design system. They only accept props and do not depend on Redux or Context.
- Located under
Components/
- Contains storybook file
<component-name>.stories.tsx
. - Follows the other conventions in ### Composed component
Component built for some parts of the application. Typically not reusable. They can be smart components that manage heavy logic or simply a normal component for visual purposes. Accepts props, redux, and context functionality depending on individual use cases.
- Created inside a folder with its name
<component-name>
in PascalCase. - The folder has a
<component-name>.tsx
file with the same name. - The file exports the React node
export function <component-name>() {...}
with the same name. - The folder contains an
index.ts
to expose the component. - May contain
<component-name>.scss
file for styling. - May contain
types.ts
to group types used by the component or sub-components. - May contain
testdata.ts
to write objects or generators for easy testing. - May contain
utils.ts
to group helper-functions and other constants.- If the file grows too big. Create a folder
utils
and group functions into named files instead.
- If the file grows too big. Create a folder
- May contain a folder
__tests__
containing a<component-name>.test.tsx
file. - May contain a folder
components
to group sub-components.- These conventions are recursive, meaning sub-components have the same structure.
- Top-level components such as pages should be suffixed with
Page
.