/
v3.0.11Get access

Get once

Calls a Swarm method once and resolves with the result. It re-runs when its dependencies change and also exposes makeRequest so you can trigger the call yourself.

Show as
Import from@softconstruct/betchain-core-client/hooks
Use it whenOne-off retrieval: profile data, static catalogs, initial balances.

Signature

.ts
useSwarmGetOnce(functionRef: TFn, options: {
  params?: ParamsOfFunction<TFn>;
  dependencies?: DependencyList;
  shouldWait?: boolean;
  successCallback?: (data?: ResponseOfFn<TFn>) => void;
}): {
  data; status; errorMessage; hasError; isLoading; makeRequest;
}

Options

Passed as the second argument, except for useInitSDK which takes a single config object.

NameTypeRequiredDescription
paramsParamsOfFunction<TFn>NoArguments forwarded to the method. Defaults to an empty object. Typed from the method you pass in.
dependenciesDependencyListNoValues that re-trigger the call when they change, like a useEffect dependency array. Defaults to an empty array, so the call runs once.
shouldWaitbooleanNoWhen true the hook does not fire on mount. Use it to defer a call until a prerequisite (an id, a logged-in session) is ready. Defaults to false.
successCallback(data?: ResponseOfFn<TFn>) => voidNoInvoked with the response after a successful call.

Returns

NameTypeRequiredDescription
dataResponseOfFn<TFn>NoThe response payload. An empty object until the first call resolves.
isLoadingbooleanNoTrue while a call is in flight.
hasErrorbooleanNoTrue when the last call failed.
messagestringNoError text for the last failure, empty otherwise.
statusLOADING | LOADED | FAILEDNoThe raw request status, if you need more than isLoading.
makeRequest(overrideParams?) => Promise<Result>NoRuns the call on demand, optionally with different params. Useful together with shouldWait.

Example

.ts
1import {2  getCompetitions3} from '@softconstruct/betchain-core-client/swarm';4import {5  useSwarmGetOnce6} from '@softconstruct/betchain-core-client/hooks';7 8const {9  data,10  isLoading,11  hasError,12  errorMessage13} = useSwarmGetOnce(getCompetitions, {14  params: {15    limit: 10,16    isFavorite: true,17    onlyLiveGames: true18  },19  dependencies: []20});

Notes

Response type follows your paramsThe return type is narrowed from the method and the params you pass, so data is typed without any casting on your side.
Was this page helpful?