Convert react-query to React.memo (#2405)
This commit is contained in:
parent
d568314f17
commit
5e28be4c18
@ -6,7 +6,7 @@ import { SubmittableExtrinsicFunction } from '@polkadot/api/types';
|
||||
import { BareProps } from '../types';
|
||||
import { DropdownOptions } from '../util/types';
|
||||
|
||||
import React from 'react';
|
||||
import React, { useCallback } from 'react';
|
||||
import ApiPromise from '@polkadot/api/promise';
|
||||
|
||||
import Dropdown from '../Dropdown';
|
||||
@ -25,7 +25,11 @@ function SelectMethod ({ api, className, isError, onChange, options, style, valu
|
||||
return null;
|
||||
}
|
||||
|
||||
const transform = (method: string): SubmittableExtrinsicFunction<'promise'> => api.tx[value.section][method];
|
||||
const transform = useCallback(
|
||||
(method: string): SubmittableExtrinsicFunction<'promise'> =>
|
||||
api.tx[value.section][method],
|
||||
[value]
|
||||
);
|
||||
|
||||
return (
|
||||
<Dropdown
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
import { SubmittableExtrinsicFunction } from '@polkadot/api/types';
|
||||
import { DropdownOptions } from '../util/types';
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
|
||||
import LinkedWrapper from './LinkedWrapper';
|
||||
@ -33,25 +33,31 @@ function InputExtrinsic ({ className, defaultValue, help, label, onChange, style
|
||||
const [optionsSection] = useState<DropdownOptions>(sectionOptions(api));
|
||||
const [value, setValue] = useState<SubmittableExtrinsicFunction<'promise'>>((): SubmittableExtrinsicFunction<'promise'> => defaultValue);
|
||||
|
||||
const _onKeyChange = (newValue: SubmittableExtrinsicFunction<'promise'>): void => {
|
||||
if (value.section === newValue.section && value.method === newValue.method) {
|
||||
return;
|
||||
}
|
||||
const _onKeyChange = useCallback(
|
||||
(newValue: SubmittableExtrinsicFunction<'promise'>): void => {
|
||||
if (!newValue || (value.section === newValue.section && value.method === newValue.method)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// set this via callback, since the we are setting a function (aletrnatively... we have issues)
|
||||
setValue((): SubmittableExtrinsicFunction<'promise'> => newValue);
|
||||
onChange(newValue);
|
||||
};
|
||||
const _onSectionChange = (section: string): void => {
|
||||
if (section === value.section) {
|
||||
return;
|
||||
}
|
||||
// set this via callback, since the we are setting a function (alternatively... we have issues)
|
||||
setValue((): SubmittableExtrinsicFunction<'promise'> => newValue);
|
||||
onChange(newValue);
|
||||
},
|
||||
[value]
|
||||
);
|
||||
const _onSectionChange = useCallback(
|
||||
(section: string): void => {
|
||||
if (section === value.section) {
|
||||
return;
|
||||
}
|
||||
|
||||
const optionsMethod = methodOptions(api, section);
|
||||
const optionsMethod = methodOptions(api, section);
|
||||
|
||||
setOptionsMethod(optionsMethod);
|
||||
_onKeyChange(api.tx[section][optionsMethod[0].value]);
|
||||
};
|
||||
setOptionsMethod(optionsMethod);
|
||||
_onKeyChange(api.tx[section][optionsMethod[0].value]);
|
||||
},
|
||||
[value]
|
||||
);
|
||||
|
||||
return (
|
||||
<LinkedWrapper
|
||||
|
||||
@ -17,7 +17,7 @@ interface Props extends BareProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
export default function AvailableDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function AvailableDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DerivedBalancesAll>(api.derive.balances.all as any, [params]);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function AvailableDisplay ({ children, className, label, params }
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(AvailableDisplay);
|
||||
|
||||
@ -17,7 +17,7 @@ interface Props extends BareProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
export default function BalanceDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function BalanceDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DerivedBalancesAll>(api.derive.balances.all as any, [params]);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function BalanceDisplay ({ children, className, label, params }:
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BalanceDisplay);
|
||||
|
||||
@ -17,7 +17,7 @@ interface Props extends BareProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
export default function BalanceFree ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function BalanceFree ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DerivedBalancesAll>(api.derive.balances.all as any, [params]);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function BalanceFree ({ children, className, label, params }: Pro
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BalanceFree);
|
||||
|
||||
@ -17,7 +17,7 @@ interface Props extends BareProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
export default function BalanceVoting ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function BalanceVoting ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DerivedBalancesAll>(api.derive.balances.all as any, [params]);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function BalanceVoting ({ children, className, label, params }: P
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BalanceVoting);
|
||||
|
||||
@ -14,7 +14,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function BestFinalized ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function BestFinalized ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const bestNumberFinalized = useCall<BlockNumber>(api.derive.chain.bestNumberFinalized as any, []);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function BestFinalized ({ children, className, label, style }: Pr
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BestFinalized);
|
||||
|
||||
@ -15,7 +15,7 @@ interface Props extends BareProps {
|
||||
withPound?: boolean;
|
||||
}
|
||||
|
||||
export default function BestNumber ({ children, className, label, style, withPound }: Props): React.ReactElement<Props> {
|
||||
function BestNumber ({ children, className, label, style, withPound }: Props): React.ReactElement<Props> {
|
||||
const { api, isApiReady } = useApi();
|
||||
const bestNumber = useCall<BlockNumber>(isApiReady && api.derive.chain.bestNumber, []);
|
||||
|
||||
@ -32,3 +32,5 @@ export default function BestNumber ({ children, className, label, style, withPou
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BestNumber);
|
||||
|
||||
@ -25,7 +25,7 @@ const byAuthor: Record<string, string> = {};
|
||||
const BlockAuthorsContext: React.Context<Authors> = React.createContext<Authors>({ byAuthor, lastHeaders: [] });
|
||||
const ValidatorsContext: React.Context<string[]> = React.createContext<string[]>([]);
|
||||
|
||||
function BlockAuthors ({ children }: Props): React.ReactElement<Props> {
|
||||
function BlockAuthorsBase ({ children }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const [state, setState] = useState<Authors>({ byAuthor, lastHeaders: [] });
|
||||
const [validators, setValidators] = useState<string[]>([]);
|
||||
@ -85,4 +85,6 @@ function BlockAuthors ({ children }: Props): React.ReactElement<Props> {
|
||||
);
|
||||
}
|
||||
|
||||
const BlockAuthors = React.memo(BlockAuthorsBase);
|
||||
|
||||
export { BlockAuthorsContext, BlockAuthors, ValidatorsContext };
|
||||
|
||||
@ -53,7 +53,7 @@ function extractTime (value?: number): Time {
|
||||
return addTime([round, 0, 0, 0], extractTime(value - (round * DAY)));
|
||||
}
|
||||
|
||||
export default function BlockToTime ({ blocks, children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function BlockToTime ({ blocks, children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const time = useMemo((): string => {
|
||||
@ -81,3 +81,5 @@ export default function BlockToTime ({ blocks, children, className, label, style
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BlockToTime);
|
||||
|
||||
@ -16,7 +16,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function BondedDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function BondedDisplay ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const controllerId = useCall<AccountId | null>(api.query.staking.bonded, [params], {
|
||||
transform: (value): AccountId | null =>
|
||||
@ -37,3 +37,5 @@ export default function BondedDisplay ({ children, className, label, params }: P
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(BondedDisplay);
|
||||
|
||||
@ -12,7 +12,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function Chain ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function Chain ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { systemChain } = useApi();
|
||||
|
||||
return (
|
||||
@ -24,3 +24,5 @@ export default function Chain ({ children, className, label, style }: Props): Re
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Chain);
|
||||
|
||||
@ -57,7 +57,7 @@ function getDisplayValue (now = 0, value: BN | Date | number = 0): string {
|
||||
|
||||
tick();
|
||||
|
||||
export default function Elapsed ({ className, style, value }: Props): React.ReactElement<Props> {
|
||||
function Elapsed ({ className, style, value }: Props): React.ReactElement<Props> {
|
||||
const [now, setNow] = useState(lastNow);
|
||||
|
||||
useEffect((): () => void => {
|
||||
@ -79,3 +79,5 @@ export default function Elapsed ({ className, style, value }: Props): React.Reac
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Elapsed);
|
||||
|
||||
@ -25,7 +25,7 @@ const MAX_EVENTS = 25;
|
||||
|
||||
const EventsContext: React.Context<Events> = React.createContext<Events>([]);
|
||||
|
||||
function Events ({ children }: Props): React.ReactElement<Props> {
|
||||
function EventsBase ({ children }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const [state, setState] = useState<Events>([]);
|
||||
|
||||
@ -57,4 +57,6 @@ function Events ({ children }: Props): React.ReactElement<Props> {
|
||||
);
|
||||
}
|
||||
|
||||
const Events = React.memo(EventsBase);
|
||||
|
||||
export { EventsContext, Events };
|
||||
|
||||
@ -16,7 +16,7 @@ interface Props extends BareProps {
|
||||
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
|
||||
}
|
||||
|
||||
export default function LockedVote ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function LockedVote ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const stakeOf = useCall<BalanceOf>(api.query.electionsPhragmen.stakeOf, [params]);
|
||||
|
||||
@ -30,3 +30,5 @@ export default function LockedVote ({ children, className, label, params }: Prop
|
||||
</FormatBalance>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(LockedVote);
|
||||
|
||||
@ -12,7 +12,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function NodeName ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function NodeName ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { systemName } = useApi();
|
||||
|
||||
return (
|
||||
@ -24,3 +24,5 @@ export default function NodeName ({ children, className, label, style }: Props):
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(NodeName);
|
||||
|
||||
@ -12,7 +12,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function NodeVersion ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function NodeVersion ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { systemVersion } = useApi();
|
||||
|
||||
return (
|
||||
@ -24,3 +24,5 @@ export default function NodeVersion ({ children, className, label, style }: Prop
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(NodeVersion);
|
||||
|
||||
@ -17,7 +17,7 @@ interface Props extends BareProps {
|
||||
params?: string | null;
|
||||
}
|
||||
|
||||
export default function Nonce ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
function Nonce ({ children, className, label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DerivedBalancesAll>(api.derive.balances.all as any, [params]);
|
||||
|
||||
@ -27,3 +27,5 @@ export default function Nonce ({ children, className, label, params }: Props): R
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Nonce);
|
||||
|
||||
@ -16,7 +16,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function TimeNow ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function TimeNow ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { api, isSubstrateV2 } = useApi();
|
||||
const timestamp = useCall<Moment>(api.query.timestamp.now, []);
|
||||
const [now, setNow] = useState<BN | undefined>();
|
||||
@ -40,3 +40,5 @@ export default function TimeNow ({ children, className, label, style }: Props):
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(TimeNow);
|
||||
|
||||
@ -13,7 +13,7 @@ interface Props extends BareProps {
|
||||
label?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function TotalIssuance ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
function TotalIssuance ({ children, className, label, style }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const totalIssuance = useCall<string>(api.query.balances.totalIssuance, []);
|
||||
|
||||
@ -31,3 +31,5 @@ export default function TotalIssuance ({ children, className, label, style }: Pr
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(TotalIssuance);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user