React - How to Display a List of Schemas/Content Types but Only Allow the User to Select One
Yes! It's definitely possible to display a list of schemas/content types and restrict selection to just one. This is exactly what the reference field type is designed for in Sanity Studio.
How it works
When you set up a reference field, you specify which document types can be referenced using the to property. Here's a basic example:
{
name: 'myReference',
type: 'reference',
to: [
{type: 'person'},
{type: 'company'},
{type: 'organization'}
]
}This will show the user a list of all three document types (person, company, organization) to choose from, but they can only select one document from those types.
Key points
- The reference field UI lets users search, browse, and select from the allowed document types
- Users can only select one document at a time (unless you wrap it in an array)
- You can filter which documents appear using the
filteroption if you need more control - By default, references are "strong" - meaning they enforce referential integrity
If you need multiple selections
If you actually want users to select multiple documents, you'd wrap the reference in an array:
{
name: 'myReferences',
type: 'array',
of: [
{
type: 'reference',
to: [{type: 'person'}, {type: 'company'}]
}
]
}But based on your question about selecting "only one," the standard reference field should be exactly what you need!
Sanity ā Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.