Skip to content

Commit 98d786e

Browse files
committed
frontend: changed return types & children types for FC
As a part of our refactoring effort in the FE to increase our code's readability and consistency as well as following the best practices and conventions, we'd like to change several things mentioned below for our functional components (FC): 1. If a component optionally accepts children we keep using `FunctionComponent` as we do already (or start to use one when it isn't used yet). 2. If a component does not accept any children we remove `FunctionComponent` and `PropsWithChildren`. 3. Remove return type `JSX.Element` or `JSX.Element | null.` 4. Children's type is mostly `ReactNode`. When component must only have exactly 1 child, the type is `ReactChild`.
1 parent 2799e57 commit 98d786e

File tree

53 files changed

+236
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+236
-218
lines changed

frontends/web/src/components/anchor/anchor.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { FunctionComponent, SyntheticEvent } from 'react';
17+
import { ReactNode, SyntheticEvent } from 'react';
1818
import { route } from '../../utils/route';
1919
import { hide } from '../guide/guide';
2020
import { debug } from '../../utils/env';
2121
import { apiPost } from '../../utils/request';
2222
import style from './anchor.module.css';
2323

24-
interface Props {
24+
type TProps = {
2525
href: string;
26+
children: ReactNode;
2627
[property: string]: any;
2728
}
2829

29-
const A: FunctionComponent<Props> = ({ href, icon, children, ...props }) => {
30+
const A = ({ href, icon, children, ...props }: TProps) => {
3031
return (
3132
<span className={style.link} onClick={(e: SyntheticEvent) => {
3233
e.preventDefault();

frontends/web/src/components/aopp/aopp.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React, { Component, PropsWithChildren } from 'react';
17+
import React, { Component, ReactNode } from 'react';
1818
import * as accountAPI from '../../api/account';
1919
import * as aoppAPI from '../../api/aopp';
2020
import { subscribe } from '../../decorators/subscribe';
@@ -30,7 +30,11 @@ import { VerifyAddress } from './verifyaddress';
3030
import { Vasp } from './vasp';
3131
import styles from './aopp.module.css';
3232

33-
const Banner = ({ children }: PropsWithChildren<{}>) => (
33+
type TProps = {
34+
children: ReactNode;
35+
}
36+
37+
const Banner = ({ children }: TProps) => (
3438
<div className={styles.banner}>{children}</div>
3539
);
3640

frontends/web/src/components/aopp/vasp.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { PropsWithChildren } from 'react';
1817
import styles from './vasp.module.css';
1918
import AOPPGroupLogo from '../../assets/exchanges/logos/aoppgroup.svg';
2019
import BitcoinSuisseLogo from '../../assets/exchanges/logos/bitcoin_suisse.png';
2120
import BittrLogo from '../../assets/exchanges/logos/bittr.png';
2221
import BityLogo from '../../assets/exchanges/logos/bity.png';
2322
import PocketBitcoinLogo from '../../assets/exchanges/logos/pocketbitcoin.svg';
2423

25-
interface VASPProps {
24+
type TVASPProps = {
2625
fallback?: JSX.Element;
2726
hostname: string;
2827
prominent?: boolean;
@@ -47,12 +46,12 @@ const VASPHostnameMap: TVASPMap = {
4746
'testing.aopp.group': 'AOPP.group',
4847
};
4948

50-
export function Vasp({
49+
export const Vasp = ({
5150
fallback,
5251
hostname,
5352
prominent,
5453
withLogoText,
55-
}: PropsWithChildren<VASPProps>) {
54+
}: TVASPProps) => {
5655
const hasLogo = hostname in VASPLogoMap;
5756
if (!hasLogo) {
5857
return fallback || (<p className={styles.hostname}>{hostname}</p>);
@@ -67,4 +66,4 @@ export function Vasp({
6766
{withLogoText ? (<p>{withLogoText}</p>) : null}
6867
</div>
6968
);
70-
}
69+
};

frontends/web/src/components/appupgraderequired.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { PropsWithChildren } from 'react';
1918
import { translate, TranslateProps } from '../decorators/translate';
2019
import A from './anchor/anchor';
2120

22-
type Props = TranslateProps;
23-
24-
function AppUpgradeRequired({ t }: PropsWithChildren<Props>): JSX.Element {
21+
function AppUpgradeRequired({ t }: TranslateProps) {
2522
return (
2623
<div className="contentWithGuide">
2724
<div className="container">

frontends/web/src/components/badge/badge.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { FunctionComponent } from 'react';
1+
import { ReactNode } from 'react';
22
import style from './badge.module.css';
33

4-
interface Props {
5-
type: string;
6-
className?: string;
4+
type TProps = {
5+
type: string;
6+
className?: string;
7+
children: ReactNode;
78
}
89

9-
export const Badge: FunctionComponent<Props> = ({ type, className, children }) => {
10+
export const Badge = ({ type, className, children }: TProps) => {
1011
return (
1112
<span className={[style.container, style[type], className].join(' ')}>
1213
{children}

frontends/web/src/components/balance/balance.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FunctionComponent } from 'react';
1918
import { useTranslation } from 'react-i18next';
2019
import { IBalance } from '../../api/account';
2120
import { FiatConversion } from '../../components/rates/rates';
2221
import { bitcoinRemoveTrailingZeroes } from '../../utils/trailing-zeroes';
2322
import style from './balance.module.css';
2423

25-
interface Props {
24+
type TProps = {
2625
balance?: IBalance;
2726
noRotateFiat?: boolean;
2827
}
2928

30-
export const Balance: FunctionComponent<Props> = ({
29+
export const Balance = ({
3130
balance,
3231
noRotateFiat,
33-
}) => {
32+
}: TProps) => {
3433
const { t } = useTranslation();
3534
if (!balance) {
3635
return (

frontends/web/src/components/banner/banner.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { PropsWithChildren } from 'react';
1817
import { subscribe } from '../../decorators/subscribe';
1918
import { translate, TranslateProps } from '../../decorators/translate';
2019
import A from '../anchor/anchor';
2120
import Status from '../status/status';
2221

23-
interface BannerInfo {
22+
type TBannerInfo = {
2423
id: string;
2524
message: { [key: string]: string; };
2625
link?: {
@@ -29,18 +28,18 @@ interface BannerInfo {
2928
};
3029
}
3130

32-
interface LoadedProps {
33-
banner: BannerInfo | null;
31+
type TLoadedProps = {
32+
banner: TBannerInfo | null;
3433
}
3534

36-
interface BannerProps {
35+
type TBannerProps = {
3736
// eslint-disable-next-line react/no-unused-prop-types
3837
msgKey: 'bitbox01';
3938
}
4039

41-
type Props = LoadedProps & BannerProps & TranslateProps;
40+
type TProps = TLoadedProps & TBannerProps & TranslateProps;
4241

43-
function Banner({ banner, i18n, t }: PropsWithChildren<Props>): JSX.Element | null {
42+
function Banner({ banner, i18n, t }: TProps) {
4443
if (!i18n.options.fallbackLng) {
4544
return null;
4645
}
@@ -57,7 +56,7 @@ function Banner({ banner, i18n, t }: PropsWithChildren<Props>): JSX.Element | nu
5756
}
5857

5958
const HOC = translate()(
60-
subscribe<LoadedProps, BannerProps & TranslateProps>(
59+
subscribe<TLoadedProps, TBannerProps & TranslateProps>(
6160
({ msgKey }) => ({ banner: 'banners/' + msgKey }),
6261
true,
6362
false,

frontends/web/src/components/centeredcontent/centeredcontent.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { FunctionComponent } from 'react';
17+
import { ReactNode } from 'react';
1818
import style from './style.module.css';
1919

20-
export const CenteredContent: FunctionComponent = ({ children }) => {
20+
type TProps = {
21+
children: ReactNode;
22+
}
23+
24+
export const CenteredContent = ({ children }: TProps) => {
2125
return (
2226
<div className="contentWithGuide">
2327
<div className={style.container}>

frontends/web/src/components/confirm/Confirm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { FunctionComponent, useRef, useState } from 'react';
17+
import { useRef, useState } from 'react';
1818
import { useTranslation } from 'react-i18next';
1919
import { SimpleMarkup } from '../../utils/markup';
2020
import { Dialog, DialogButtons } from '../dialog/dialog';
@@ -38,7 +38,7 @@ interface State {
3838
* Confirm alert that activates on confirmation module export call,
3939
* this should be mounted only once in the App
4040
*/
41-
export const Confirm: FunctionComponent = () => {
41+
export const Confirm = () => {
4242
const [state, setState] = useState<State>({ active: false });
4343
const { t } = useTranslation();
4444
const callback = useRef<TCallback>(() => {});

frontends/web/src/components/devices/bitbox02bootloader/toggleshowfirmwarehash.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ChangeEvent, FunctionComponent, useState } from 'react';
17+
import { ChangeEvent, useState } from 'react';
1818
import { useTranslation } from 'react-i18next';
1919
import { load } from '../../../decorators/load';
2020
import { apiPost } from '../../../utils/request';
2121
import { Toggle } from '../../toggle/toggle';
2222

23-
interface ToggleProps {
23+
type TToggleProps = {
2424
deviceID: string;
2525
}
2626

27-
interface LoadedProps {
27+
type TLoadedProps = {
2828
enabled: boolean;
2929
}
3030

31-
type Props = ToggleProps & LoadedProps;
31+
type TProps = TToggleProps & TLoadedProps;
3232

33-
const ToggleFWHash: FunctionComponent<Props> = ({ enabled, deviceID }) => {
33+
const ToggleFWHash = ({ enabled, deviceID }: TProps) => {
3434
const { t } = useTranslation();
3535
const [enabledState, setEnabledState] = useState<boolean>(enabled);
3636

@@ -57,5 +57,5 @@ const ToggleFWHash: FunctionComponent<Props> = ({ enabled, deviceID }) => {
5757
);
5858
};
5959

60-
const HOC = load<LoadedProps, ToggleProps>(({ deviceID }) => ({ enabled: 'devices/bitbox02-bootloader/' + deviceID + '/show-firmware-hash-enabled' }))(ToggleFWHash);
60+
const HOC = load<TLoadedProps, TToggleProps>(({ deviceID }) => ({ enabled: 'devices/bitbox02-bootloader/' + deviceID + '/show-firmware-hash-enabled' }))(ToggleFWHash);
6161
export { HOC as ToggleShowFirmwareHash };

0 commit comments

Comments
 (0)