Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Accessing document _id in custom component for querying in Sanity

2 replies
Last updated: Oct 23, 2024
Hi there, I am trying to get the _id of a document inside of a custom component so I can query using that _id. I have tried making a custom input / field but none of the props have the _id (value, document). Does anyone know if this is possible? The code will look something like this to display the custom component:
const GuidesWithChunksInput: React.FC<GuidesWithChunksInputProps> = (props) => {
  const { value, document } = props;
  return (
    <div>
      {document && document._id ? (
        <GuidesWithChunks chunkId={document._id} />
      ) : (
        <div>No chunk ID available</div>
      )}
    </div>
  );
};
Oct 23, 2024, 3:16 AM
Schema is looking like this:
{
      name: 'guidesWithChunks',
      type: 'string',
      title: 'Guides containing this chunk:',
      components: {
        field: GuidesWithChunksInput,
      },
    },
Oct 23, 2024, 3:16 AM
nvm it was
useFormValue(['_id'])
Oct 23, 2024, 11:14 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?