Communities Hooks
Communities Hooks in Bitsocial React Hooks. Usage, options, and examples.
useCommunity({community: {name?: string, publicKey?: string}, onlyIfCached?: boolean, autoUpdate?: boolean}): Community & {syncState: "initializing" | "loading" | "retrying" | "succeeded" | "failed" | "stopped", hasCachedData: boolean, firstLoadFailed: boolean, lastFetchAttemptAt: number | undefined, lastSuccessfulFetchAt: number | undefined, lastRetriableError: Error | undefined}
useCommunities({communities?: CommunityIdentifier[], onlyIfCached?: boolean, autoUpdate?: boolean}): {communities: (Community | undefined)[], communityLoadStatuses: CommunityLoadStatus[]}
// CommunityLoadStatus: {state: string, syncState: CommunitySyncState, hasCachedData: boolean, firstLoadFailed: boolean, lastFetchAttemptAt: number | undefined, lastSuccessfulFetchAt: number | undefined, lastRetriableError: Error | undefined, error: Error | undefined, errors: Error[]}
useCommunityStats({community: {name?: string, publicKey?: string}, onlyIfCached?: boolean}): CommunityStats
useResolvedCommunityAddress({communityAddress: string, cache: boolean}): {resolvedAddress: string | undefined} // use {cache: false} when checking the user's own community address
Pass { publicKey, name } when you have both so pkc-js can fetch through the public key and resolve the name in the background. communityAddress, communityAddresses, and communityRefs are no longer accepted by these hooks.
useCommunity only exposes community error events that are not superseded by a later update event. Transient fetch errors are delayed briefly before surfacing through error or errors.
useCommunity().state remains succeeded when cached community data is available, even
while a refresh is running. Use syncState for the current refresh lifecycle:
loading covers active name, IPNS, and IPFS work, while retrying means a transient
failure is being retried. lastFetchAttemptAt and lastSuccessfulFetchAt are Unix
timestamps in seconds. Transient failures stay out of error and errors;
lastRetriableError holds the latest one until the community updates, so a community
that never loads can show why. A successful fetch only proves that a valid community record was
reachable; it does not prove that the community operator is currently online.
firstLoadFailed is true while the community has no record (hasCachedData is false) and
a load of it has failed: the load threw, community.update() rejected, pkc-js reported
failed, or it is retrying after an error. pkc-js retries an unreachable community forever, so
syncState alternates between loading and retrying and never settles on failed;
firstLoadFailed stays true through those retries and becomes false once a record loads.
state is failed while the community has no record and its load failed non-retriably: it threw
before the community reached the store (instead of staying initializing), community.update()
rejected (instead of ending in stopped), or pkc-js reported failed. It stays failed
through the next attempts until a record loads. A community that is only retrying keeps its
loading state, and a community the hooks stopped because nothing used it is not failed.
useCommunities().communities has an entry only for a community that reached the store, and its
aggregate state is failed only while a community missing from the store has errors, and
succeeded once every community reached the store, with or without a record, so neither tells
which community failed or whether it is retrying. communityLoadStatuses reports the
useCommunity load fields (state, syncState, hasCachedData, firstLoadFailed,
lastFetchAttemptAt, lastSuccessfulFetchAt, lastRetriableError, error, errors) for each
requested community, at the same index as in the communities option, with an entry for every
community, and the same values useCommunity returns for it. A status object keeps its reference
until that community’s status changes, and the array keeps its reference until one of them does.
A community keeps updating while a useCommunity, useCommunities, useCommunityStats,
useCommunitiesStates, useAccountCommunities, useFeed, or useBufferedFeeds that uses it is
mounted, and stops updating 2 minutes after the last one unmounts.
Mounting one again restarts its updates: the store keeps the last record, and useCommunity
reports syncState loading until the next fetch, with lastSuccessfulFetchAt still the time of
the last one. Every restart downloads the whole community record again, which the 2 minute delay
avoids during quick back-and-forth navigation. autoUpdate: false loads communities once
without keeping them updating, and onlyIfCached: true reads only communities already loaded.
A community read with onlyIfCached while nothing keeps it updating has syncState stopped.
Code that reads the stores directly gets the last stored record of a stopped community; it
restarts only when one of these hooks mounts, and useFeed().reset() fetches a fresh record.