Skip to content

Commit

Permalink
Add "Null checks" section in frontend style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbroman committed Mar 2, 2023
1 parent 2bc29cc commit 6590bcd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .docs/FRONTEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ export const MyComponent = observer(MyComponentPresenter);
```

This makes sure `eslint-plugin-react-hooks` identifies the React component and will lint its hooks correctly. We use the `Presenter` suffix to indicate that the component is unwrapped and should not be used directly.

## Component Null Checks

Any nullable value that is critical to the component should be null checked before being used:

```tsx
const MyComponent = ({ prop }: { prop: string | null }) => {
if (!prop) return null;

return <div>{prop}</div>;
};
```

Note that the component might need to broken down into smaller components to achieve this without calling hooks conditionally.

Alternatively, if a nullable value is not critical to the component, it can be defaulted to a placeholder value (e.g. `''` or `[]`).

Avoid using `!` to assert that a value is not null as it will cause a runtime error if the value is null.

0 comments on commit 6590bcd

Please sign in to comment.