Get a community
Get a community in Bitsocial React Hooks. Usage, options, and examples.
From 0.5.0, .bso lookups use the BSIP-13 registry and its resolver normalization. Use the current recorded name or the community public key in app state and routes. Community names are compared exactly, including case and suffix. A comment signed for board.eth does not belong to the board.bso feed, even if the names have used the same community key. For standalone reply pages, the exact comparison uses the name signed by the parent comment.
const community = useCommunity({ community: { name: communityAddress, publicKey: communityPublicKey } });
const {
syncState,
hasCachedData,
firstLoadFailed,
lastFetchAttemptAt,
lastSuccessfulFetchAt,
lastRetriableError,
} = community;
const communityStats = useCommunityStats({
community: { name: communityAddress, publicKey: communityPublicKey },
});
const { communities } = useCommunities({
communities: [
{ name: communityAddress, publicKey: communityPublicKey },
{ name: communityAddress2, publicKey: communityPublicKey2 },
{ name: communityAddress3, publicKey: communityPublicKey3 },
],
});
// mounted communities keep following community IPNS updates, and stop
// updating 2 minutes after the last hook using them unmounts
// how each community of a multi-community feed is loading, at the index of its identifier
const { communityLoadStatuses } = useCommunities({ communities: feedCommunities });
const loadedCommunities = feedCommunities.filter(
(_, index) => communityLoadStatuses[index].hasCachedData,
);
// no community has a record and every one failed to load non-retriably
const feedFailed =
communityLoadStatuses.length > 0 &&
communityLoadStatuses.every(({ state }) => state === "failed");
// the communities without a record whose load failed, terminally or while retrying, and why
const failedLoadErrors = communityLoadStatuses
.filter(({ firstLoadFailed }) => firstLoadFailed)
.map(({ error, lastRetriableError }) => error || lastRetriableError);
// load communities once, e.g. to read their roles, without keeping them updating
const { communities: loadedOnceCommunities } = useCommunities({
communities: [{ name: communityAddress, publicKey: communityPublicKey }],
autoUpdate: false,
});
// use without affecting performance
const { communities: cachedCommunities } = useCommunities({
communities: [
{ name: communityAddress, publicKey: communityPublicKey },
{ name: communityAddress2, publicKey: communityPublicKey2 },
{ name: communityAddress3, publicKey: communityPublicKey3 },
],
onlyIfCached: true,
});
// community.posts are not validated, to show posts
const { feed, hasMore, loadMore } = useFeed({
communities: [{ name: communityAddress, publicKey: communityPublicKey }],
});
// to show a preloaded post without rerenders, validate manually
const { valid } = useValidateComment({ comment: community.posts.pages.topAll.comments[0] });
if (valid === false) {
// don't show this post, it's malicious
}
// won't cause any rerenders if true