Skip to content

Instantly share code, notes, and snippets.

@mwmwmw
Last active October 4, 2021 16:09
Show Gist options
  • Save mwmwmw/d62fdcd57f6c2fddb5e23d23f91f35f9 to your computer and use it in GitHub Desktop.
Save mwmwmw/d62fdcd57f6c2fddb5e23d23f91f35f9 to your computer and use it in GitHub Desktop.
A React hook for grabbing assets from OpenSea
import { useEffect, useState } from 'react';
const options = { method: 'GET' };
export default function useOpenSeaAssets({
owner,
sortDirection = 'asc',
offset = 0,
limit = 50,
collection,
}) {
const [assets, setAssets] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
fetch(
`https://api.opensea.io/api/v1/assets?owner=${owner}&order_direction=${sortDirection}&offset=${offset}&limit=${limit}&collection=${collection}`,
options,
)
.then((response) => response.json())
.then((res) => res.assets || [])
.then((response) => setAssets(response))
.then(() => setLoading(false))
.catch((err) => console.error(err));
}, [owner, sortDirection, collection, offset, limit]);
return {
assets,
loading,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment