Hook to fetch and load the content uri of a NFT. Either returns the uri to fetch or the content that is contained in the uri in the case of text posts.
This hook makes a request to fetch metadata from IPFS retrieved from the zNFT metadataURI.
Most IPFS servers allow remote JSON fetches, including all Zora NFTs. There is a chance this request could fail when the server does not allow cross-origin requests. Requests are set with a 15 second timeout to allow showing the user an error message instead of an indefinite loader.
Hook result type:
// This is a union type meaning one or another in typescript.// Use the (media.type) to determine which type was returned from the hook.typeMediaContentType=| { uri:string; // URI of content to render, used for media type:'uri',// Shows that no content was downloaded, should only render mimeType:string// mime type string from metadata for rendering }| { text:string; // Text of result type:'text',// Shows that text content to render was downloaded mimeType:string// mime type string from metadata for rendering };typeuseNFTContentType= { loading:boolean; // If loading from the server error?:string; // Error returned from network request error or timeout content?:MediaContentType; // MediaConetentType shown above;}
To use the hook, simply pass in the contentURI from the zNFT on-chain data and mimeType from the NFT Metadata.
If you do not have access to mimeType from the metadata or do not wish to retrieve the metadata, the mimeType can be omitted with a small performance impact.
Content returned from this hook is not cached, each time the hook is used the content is fetched.
Alternatively, the same information can be fetched using the base MediaFetchAgent for server-side or non-react use:
import {MediaFetchAgent} from"@zoralabs/nft-hooks";// Get result from the serverconstresult=awaitMediaFetchAgent.fetchContent("https://ipfs.io/ipfs/METADATA_HASH","application/json");// result type is MediaContentType