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 Replyke entity ID

import { EntityProvider } from "@replyke/react-js";

<EntityProvider entityId="ent_abc123">
  <EntityDetail />
</EntityProvider>
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.
<EntityProvider foreignId="my-system-id-456" createIfNotFound>
  <EntityDetail />
</EntityProvider>
3

By short ID

Short IDs are auto-generated by Replyke and are useful for short sharing URLs.
<EntityProvider shortId="x7k2m">
  <EntityDetail />
</EntityProvider>
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 entity={myEntity}>
  <EntityDetail />
</EntityProvider>
EntityProvider renders null if none of the identifier props are provided. Exactly one must be present.

Accessing Entity State with useEntity

Inside any child of EntityProvider, call useEntity() to access the entity and its actions:
import { useEntity } from "@replyke/react-js";

function EntityDetail() {
  const { entity, updateEntity, deleteEntity } = useEntity();

  if (!entity) return <p>Loading...</p>;

  return (
    <div>
      <h1>{entity.title}</h1>
      <p>{entity.content}</p>
      <button onClick={() => deleteEntity()}>Delete</button>
    </div>
  );
}

useEntity Return Values

entity
Entity | null | undefined
The loaded entity. undefined while loading, null if not found.
setEntity
React.Dispatch<SetStateAction<Entity | null | undefined>>
Direct state setter. Use this if you need to manually update the local entity state.
updateEntity
(props: { update: UpdateEntityUpdate }) => Promise<Entity | undefined>
Updates the entity and syncs the new state locally. Accepts:
  • 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
deleteEntity
() => Promise<void>
Deletes the entity and sets local state to undefined.

EntityProvider Props Reference

entityId
string
Replyke entity ID. Mutually exclusive with foreignId, shortId, and entity.
foreignId
string
Your system’s ID for this entity. Mutually exclusive with the others.
shortId
string
Auto-generated short identifier. Mutually exclusive with the others.
entity
Entity
A pre-fetched entity object. Pass this to avoid a redundant fetch. Mutually exclusive with the others.
createIfNotFound
boolean
Only valid with foreignId. If true, the entity is automatically created when it doesn’t exist yet. Useful for “entity-per-resource” patterns.