Studio

Incoming reference decoration

Display incoming references directly in a document's form, without storing the data as part of the document.

Sanity Studio's inline incoming references feature lets you define a component that displays any incoming references to the current document.

Unlike a field containing an array of references, these incoming references are not part of the document and display automatically when new references are made.

Prerequisites:

  • Studio v5.8.0 or later is required to use incoming reference fields.

Add an inline incoming reference decoration

To add incoming references to a document type, return defineIncomingReferenceDecoration from the type's renderMembers function.

This adds a component to the document form that looks like the following:

Loading...

Create and reference new documents

You can allow the Create button to pre-populate a new document with a reference to the source document.

This feature can use the initialValue of a new document to set the reference. On the schema for the referencing document type, use the isIncomingReferenceCreation helper to check if an incoming reference field is creating the new document. Then pass in the reference to the appropriate field. In this example, the book document type references the author type.

Link existing documents

If you have existing documents that you want to search for and assign to the current document, use the onLinkDocument option.

Create custom actions

You can also pass custom actions to the incoming reference decoration. This format is similar to document actions, but occurs in the defineIncomingReferenceDecoration helper. This example creates an action in ReferenceActions.tsx and applies it in the actions array.

Support cross-dataset references

Cross-dataset references are also supported. For each type in another dataset, set dataset and preview, and optionally title and studioUrl:

The studioUrl function receives the referencing document's id and type and returns a URL for that document. The incoming reference preview opens the URL in a new tab, and shows no link if the function returns null. Point the URL at the studio workspace that edits the other dataset. In this example, that workspace is mounted at /us.

Configure additional options

The defineIncomingReferenceDecoration helper also accepts the following options:

  • Text displayed below the decoration's title.

  • filter

    string | function

    A GROQ filter applied to incoming references in addition to the type filter. The _type filter is applied automatically. To build the filter from the current document, pass a function that receives {document, getClient} and returns a filter string or {filter, filterParams}.

  • filterParams

    Record<string, string>

    Parameters for the GROQ filter. When a filter function returns its own filterParams, those are used instead.

  • creationAllowed

    boolean | string[]

    Controls the Create button. Defaults to true, which allows creating documents from any initial value template of the type. Set to false to only allow linking existing documents, which requires onLinkDocument. Pass an array of template IDs to allow only those templates.

Next steps

Was this page helpful?