Skip to main content
EntityProvider is the standard way to load and work with a single entity. Wrap a component tree with it, then use useEntity in any descendant to access the entity’s data and actions.

Setting Up EntityProvider

EntityProvider accepts one of four mutually exclusive identifier props:
1

By Sublay entity ID

2

By foreign ID

Use this when your entity is linked to an item in your own system. Pass createIfNotFound to auto-create the entity if it doesn’t exist yet.
3

By short ID

Short IDs are auto-generated by Sublay and are useful for short sharing URLs.
4

By passing an entity object

If you already have the full entity object (e.g., from a list), pass it directly to skip the initial fetch.
EntityProvider renders null if none of the identifier props are provided. Exactly one must be present.
When it fetches by entityId, foreignId or shortId, EntityProvider takes the same include, topCommentsLimit and topCommentsSortByReaction options as the entity fetch hooks, and sends them with its fetch (with createIfNotFound too):
See The include parameter for the values. topCommentsLimit and topCommentsSortByReaction are sent only when include contains "topComments". Changing any of the three fetches the entity again with the new options; the entity on screen stays until the new one arrives. If that fetch fails, the entity on screen stays and the error is reported through the SDK’s error handler. The provider keeps each entity it fetched by identifier and options, so going back to options it already used shows that entity without a request. A change made to the entity on screen (an edit, a new comment count) is kept with it; the entity under other options is fetched again the next time they are used, so it never shows the entity from before the change. An include given inline (include={["user"]}) is not a change on each render. With the entity prop the options are ignored: the entity is used as given. They can still be passed, so one set of props can carry either an entity or an id. When the signed-in user signs in, signs out or switches accounts, EntityProvider fetches its entity again (when it was given an id), with the same options, since the entity carries that user’s own reaction (userReaction). An entity passed as the entity prop is used as it is.

Accessing Entity State with useEntity

Inside any child of EntityProvider, call useEntity() to access the entity and its actions:

useEntity Return Values

Entity | null | undefined
The loaded entity. undefined while loading, null if not found.
React.Dispatch<SetStateAction<Entity | null | undefined>>
Direct state setter. Use this if you need to manually update the local entity state.
(changes: Omit<UpdateEntityProps, "entityId">) => Promise<Entity | undefined>
Updates the provider’s entity and syncs the new state locally. Takes the changes alone, e.g. updateEntity({ title }):
  • title — new title
  • content — new content
  • attachments — array of attachment objects
  • keywords — array of keyword strings
  • location — { latitude, longitude }
  • metadata — arbitrary JSON object
  • mentions — array of Mention objects
  • nsfw — the entity’s own NSFW flag
  • images, files — files to append (sends multipart), and removeFileIds — ids of the entity’s files to remove
  • include — "files" for the entity’s files in the response
The same fields as useUpdateEntity, without entityId.
() => Promise<void>
Deletes the entity and sets local state to undefined.

EntityProvider Props Reference

string
Sublay entity ID. Mutually exclusive with foreignId, shortId, and entity.
string
Your system’s ID for this entity. Mutually exclusive with the others.
string
Auto-generated short identifier. Mutually exclusive with the others.
Entity
A pre-fetched entity object. Pass this to avoid a redundant fetch. Mutually exclusive with the others.
boolean
Only valid with foreignId. If true, the entity is automatically created when it doesn’t exist yet. Useful for “entity-per-resource” patterns. The created entity has no author, so the project’s Who can create entities without an author setting decides who may create it: everyone by default. A caller the setting refuses gets 403 entity/authorless-creation-not-allowed. See Fetch Entity by Foreign ID.
EntityIncludeParam
Ignored with entity. Related data to populate on the fetched entity, as a single value or an array: "user", "space", "topComments", "saved", "files", "grants". Changing it fetches the entity again.
number
Ignored with entity. With "topComments" included: how many top comments to attach. Server default 1, maximum 5. Not sent when include doesn’t contain "topComments".
ReactionType
Ignored with entity. With "topComments" included: the reaction whose raw count ranks the top comments. Server default "upvote". Not sent when include doesn’t contain "topComments".