Sanity Annotation Renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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