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.Loading Related Data
When it fetches byentityId, 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):
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
Direct state setter. Use this if you need to manually update the local entity state.
Updates the provider’s entity and syncs the new state locally. Takes the changes alone, e.g.
updateEntity({ title }):title— new titlecontent— new contentattachments— array of attachment objectskeywords— array of keyword stringslocation—{ latitude, longitude }metadata— arbitrary JSON objectmentions— array ofMentionobjectsnsfw— the entity’s own NSFW flagimages,files— files to append (sends multipart), andremoveFileIds— ids of the entity’s files to removeinclude—"files"for the entity’s files in the response
useUpdateEntity, without entityId.EntityProvider Props Reference
A pre-fetched entity object. Pass this to avoid a redundant fetch. Mutually exclusive with the others.
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.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.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".
