← 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>.tsxfile with the same name. - The file exports the React node
export function <component-name>() {...}with the same name. - The folder contains an
index.tsto expose the component. - May contain
<component-name>.scssfile for styling. - May contain
types.tsto group types used by the component or sub-components. - May contain
testdata.tsto write objects or generators for easy testing. - May contain
utils.tsto group helper-functions and other constants.- If the file grows too big. Create a folder
utilsand 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.tsxfile. - May contain a folder
componentsto 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.