RD Pennell
Community Engineer at Sanity.io
RD is located at Richmond, CA
Use the renderDefault method to show or hide fields.
import { ConditionalComponent } from './path/to/component'
//...
{
name: 'reference',
type: 'reference',
to: [{ type: 'referenceDocument' }],
},
{
name: 'sibling',
type: 'string',
components: {
field: ConditionalComponent,
},
},
import { useEffect, useState } from 'react';
import { useClient, useFormValue } from 'sanity';
const ConditionalComponent = props => {
const [hideSibling, setHideSibling] = useState(false);
const { renderDefault } = props;
const client = useClient({ apiVersion: '2023-04-01' });
const reference = useFormValue(['reference'])._ref;
useEffect(() => {
if (!reference) return;
client
.fetch(`*[_id == $reference].hideSibling`, { reference })
.then(setHideSibling);
}, [client, reference]);
return hideSibling ? null : renderDefault(props);
};
The hidden
callback can't handle promises, so you'll need to use a custom component to access the Studio client. Thankfully, the renderDefault
method that's passed into your component's props
makes this doable in a few lines of code.
Community Engineer at Sanity.io
This will allow you to display an array of references as a checklist from which you can multi-select.
Go to V3 version of Display an array of references as a checklistPopulate your list options in a string schema using an external API
Go to Asynchronous List Options ComponentUse the renderDefault function to easily control your available array options.
Go to Filter Array Options Based on the Current User RoleFetch an image from a URL and upload to Sanity it via the JS Client
Go to Upload An Image From a URL