useFeed
Hook
The useFeed
hook provides access to all the context and functionality exposed by the FeedProvider
. It allows developers to interact with the feed’s data, manage its state, and dynamically modify filters. Below is the interface for all available values and functions:
export interface UseFeedDataValues {
entities: Entity[];
infusedEntities: (Entity & Record<string, any>)[];
loading: boolean;
hasMore: boolean;
resetting: boolean;
sortBy: FeedSortByOptions | null;
setSortBy: (sortBy: FeedSortByOptions) => void;
timeFrame: TimeFrame | null;
setTimeFrame: (timeFrame: TimeFrame | null) => void;
followedOnly: boolean;
setFollowedOnly: (state: boolean) => void;
userId: string | null;
setUserId: (userId: string | null) => void;
keywordsFilters: KeywordsFilters | null;
updateKeywordsFilters: (
type: "add" | "remove" | "reset",
key: "includes" | "doesNotInclude" | "both",
value?: string | string[]
) => void;
metadataFilters: MetadataFilters | null;
setMetadataFilters: (metadata: MetadataFilters | null) => void;
titleFilters: TitleFilters | null;
setTitleFilters: (metadata: TitleFilters | null) => void;
contentFilters: ContentFilters | null;
setContentFilters: (metadata: ContentFilters | null) => void;
mediaFilters: MediaFilters | null;
setMediaFilters: (metadata: MediaFilters | null) => void;
locationFilters: LocationFilters | null;
setLocationFilters: (location: LocationFilters | null) => void;
loadMore: () => void;
resetEntities: () => Promise<void>;
createEntity: (props: {
referenceId?: string;
title?: string;
content?: string;
media?: any[];
keywords?: string[];
location?: {
latitude: number;
longitude: number;
};
metadata?: Record<string, any>;
insertPosition?: "first" | "last";
}) => Promise<void>;
deleteEntity: (props: { entityId: string }) => Promise<void>;
}
Explanation of Hook Values and Functions
Entities and Data Management
entities
: Contains an array of all entities fetched for the feed.infusedEntities
: If aninfuseData
function is provided to the provider, this array contains entities with additional external data merged. Theentities
array remains “pure,” holding only the data fetched by Replyke.loading
: A boolean flag indicating whether entities are currently being fetched. Useful for showing loading indicators in the UI.hasMore
: Indicates whether there are more entities to fetch. Iffalse
, the feed has reached its end and there is no more data to be fetched.resetting
: A flag indicating that the feed is currently being reset. Whenresetting
is true,loading
will also be true, but not vice versa.
Sorting
sortBy
: Holds the current sorting criteria for the feed.setSortBy
: Updates the sorting criteria dynamically.
Filter States and Functions
The following values represent the current state of feed filters, and corresponding functions allow dynamic updates. Each filter change will reset the feed.
timeFrame
: The current state of the time frame filter.setTimeFrame
: Updates the time frame filter.followedOnly
: A boolean indicating whether the feed is limited to entities from followed accounts.setFollowedOnly
: Updates thefollowedOnly
filter.userId
: The current user ID filter.setUserId
: Updates the user ID filter.keywordsFilters
: The current keywords filter state.updateKeywordsFilters
: Dynamically updates keywords filters by adding, removing, or resetting values.titleFilters
: The current state of title filters.setTitleFilters
: Updates the title filters.contentFilters
: The current state of content filters.setContentFilters
: Updates the content filters.mediaFilters
: The current state of media filters.setMediaFilters
: Updates the media filters.locationFilters
: The current state of location filters.setLocationFilters
: Updates the location filters.metadataFilters
: The current state of metadata filters.setMetadataFilters
: Updates the metadata filters.
Actions and Mutations
loadMore
: Fetches additional entities for the feed.resetEntities
: Resets the feed, refreshing data without altering filter states.createEntity
: Creates a new entity within the feed context. The newly created entity is automatically added to the feed.deleteEntity
: Deletes an entity within the feed context. The deleted entity is automatically removed from the feed.
This hook gives you complete control over managing and customizing the feed’s behavior, enabling a wide range of use cases. In the following chapters, we’ll explore practical examples of implementing the FeedProvider
with useFeed
using filters in our app.