Skip to content

Instantly share code, notes, and snippets.

@troyatomic
Last active August 8, 2022 19:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save troyatomic/8fc3b035b3a47d5cd417ab9afc73a253 to your computer and use it in GitHub Desktop.
Sanity Annotation Renderer
import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import sanityClient from 'part:@sanity/base/client'
const apiVersion = `2021-05-19`
const client = sanityClient.withConfig({ apiVersion })
let count = 0;
const ProductRenderer = props => {
const [product, setProduct] = useState({});
useEffect(() => {
async function fetchProduct() {
try {
const res = await client.getDocument(props._ref);
setProduct(res);
} catch (err) {
console.log(err);
}
}
if (count < 5) {
fetchProduct();
count++;
}
});
return <span>{props.children} ({product?.name})</span>
}
ProductRenderer.propTypes = {
children: PropTypes.node.isRequired,
_ref: PropTypes.node.isRequired,
}
export default ProductRenderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment