Skip to content

Commit ca37cf6

Browse files
committed
Remove useArgs() function
1 parent a0d1274 commit ca37cf6

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

src/components/content/home-page/blocks/block.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use client';
22

33
import { CubeIcon } from '@heroicons/react/24/outline';
4-
import { type Block } from 'alchemy-sdk';
4+
import type { Alchemy, Block } from 'alchemy-sdk';
55
import {
66
getSecsFromUnixSecs,
77
getBlockAgeFromSecs,
88
truncateAddress,
99
useAlchemy,
1010
} from '@/lib/utilities';
11-
import { useDataState, useArgs, DataState } from '@/lib/data-state';
11+
import { useDataState, DataState } from '@/lib/data-state';
1212
import Link from 'next/link';
1313
import PopoverLink from '../../../common/popover-link';
1414
import LoadingPulse from '@/components/common/indicators/loading-pulse';
@@ -23,11 +23,11 @@ export default function Block(props: {
2323
network: string,
2424
}) {
2525
const alchemy = useAlchemy(props.network);
26-
const blockNumber = props.latestBlockData.value ? props.latestBlockData.value - props.id : undefined;
26+
const blockNumber = props.latestBlockData.value && props.latestBlockData.value - props.id;
2727

28-
const blockData = useDataState({
28+
const blockData = useDataState<Block, [Alchemy, number?]>({
2929
fetcher: (alchemy, num) => alchemy.core.getBlock(num!),
30-
args: useArgs(alchemy, blockNumber),
30+
args: [alchemy, blockNumber],
3131
});
3232

3333
return (

src/components/content/home-page/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Transactions from './transactions';
77
import Stats from './stats';
88
import NodeBanner from './node-banner';
99
import { useAlchemy } from '@/lib/utilities';
10-
import { useDataState, useArgs } from '@/lib/data-state';
10+
import { useDataState } from '@/lib/data-state';
1111

1212

1313
export default function HomePage(props: {network: string}) {

src/components/content/home-page/transactions/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22

33
import { useAlchemy } from '@/lib/utilities';
4-
import { useDataState, useArgs, DataState } from '@/lib/data-state';
4+
import { useDataState, DataState } from '@/lib/data-state';
5+
import type { Alchemy, BlockWithTransactions } from 'alchemy-sdk';
56
import Transaction from './transaction';
67
import ErrorWithRefetch from '@/components/common/indicators/error-with-refetch';
78

@@ -12,9 +13,9 @@ export default function Transactions(props: {
1213
}) {
1314
const alchemy = useAlchemy(props.network);
1415

15-
const blockWithTransactionsData = useDataState({
16+
const blockWithTransactionsData = useDataState<BlockWithTransactions, [Alchemy, number?]>({
1617
fetcher: (alchemy, num) => alchemy.core.getBlockWithTransactions(num!),
17-
args: useArgs(alchemy, props.latestBlockData.value),
18+
args: [alchemy, props.latestBlockData.value],
1819
});
1920

2021
let transactionsDisplay;

src/lib/data-state.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,6 @@ export const DataState = {
181181
}
182182
};
183183

184-
// Hook that memoizes the argument array to prevent a new array
185-
// from being created on every render:
186-
// (For inline use in `useDataState()` or `DataState.Init()` calls)
187-
export function useArgs<A extends any[]>(...args: A): A {
188-
// Prevent infinite loop by NOT refetching unless the arguments actually changed:
189-
// eslint-disable-next-line react-hooks/exhaustive-deps
190-
return useMemo(() => args, args);
191-
}
192-
193184
// To be used as a wrapper for fetch() inside useDataState inline fetcher definition:
194185
// (This allows the types to match with the DataState's, instead of returning a fetch Response type)
195186
export async function fetchJson<T>(...args: any[]): Promise<T> {
@@ -198,15 +189,17 @@ export async function fetchJson<T>(...args: any[]): Promise<T> {
198189

199190
const json = await response.json();
200191
const result = 'result' in json ? json.result : json;
201-
if (!result) throw new FetchError(`Empty json response or result, json: ${JSON.stringify(json)}`);
192+
if (!result) throw new FetchError(`Empty json response or result.
193+
json: ${JSON.stringify(json)}
194+
URL: ${args[0]}`);
202195

203196
return result as T;
204197
}
205198

206199
export class FetchError extends Error {
207200
constructor(message: string) {
208201
super(message);
209-
this.name = 'FetchWithoutJsonError';
202+
this.name = 'FetchError';
210203
}
211204
}
212205

0 commit comments

Comments
 (0)