Components
One React component per file
- Never use inline React components.
- Each React component must be defined as a separate function in its own file.
- Render helpers count too: a
renderWarning()orgetBanner()that returns JSX is a component in disguise, so extract it into its own component instead of calling it from the render.
Bad:
export const Foo = () => {
const Bar = () => <div>Bar</div>;
return (
<div>
<Bar />
</div>
);
};
const Bar = () => <div>Bar</div>;
export const Foo = () => {
return (
<div>
<Bar />
</div>
);
};
export const Foo = () => {
const renderBar = () => <div>Bar</div>;
return <div>{renderBar()}</div>;
};
Good:
// File: Bar.tsx or Bar/Bar.tsx
export const Bar = () => <div>Bar</div>;
// File: Foo.tsx or Foo/Foo.tsx
import { Bar } from './Bar';
export const Foo = () => (
<div>
<Bar />
</div>
);
Component File structure
- Imports
- Styles
- Component constants
- Component helpers
- Component types
- Component
- Component helpers, Component types, Styles: If there's more of them or they're not straightforward, consider moving them to a separate file to keep the component file clean.
- For web and desktop, prefer components from
@trezor/componentsand@trezor/product-components, configured through their public props. For native UI, follow the owning package’s native components and styling conventions. - Reach for
styledvery rarely: try to reuse existing components instead for web/desktop from@trezor/componentsand@trezor/product-components, don't take the existingstyledcode inpackages/suiteandsuite/as a pattern. Inside the two libraries, styled components count as styles rather than components, so they can stay in the component file or move toFoo.styles.ts. - Nest a child component inside its parent's directory when only that parent renders it, so the path shows the relationship. A component that several parents render moves up to their closest common directory, the same way shared hooks do.
Component structure
The following structure is just a recommendation, in fact it's not even always possible to keep the same order inside a component. However, trying to be consistent really helps in the long run, especially when it comes to navigating larger components. It is also useful to group things within each category, e.g. the refs might not have empty lines between each other but if there is only one useDispatch better surround it with them.
- Redux selectors _(aka global state)_
useState_(aka local state)_- Non-effect hooks:
useRef,useForm,useDispatch, etc
1. This one is tricky. Consistency among the non-effect hook order is perhaps too redundant, although I would always put useForm first, for example. Try to place them in the order of subjective importance.
- Effects
- Functions / callbacks
- Values
- Render
Generally, it's considered a _good_ practice to not use optimisation techniques until you see that you need them. In a tree the React Compiler compiles there is a stronger rule: don't add useMemo, useCallback or memo() at all — auto-memoization already covers it, and a hand-written memo whose equivalence the compiler cannot prove fails react-hooks/preserve-manual-memoization, which is error both locally and on CI. Which trees are compiled is defined by REACT_COMPILER_PATHS in