Last active
October 4, 2021 16:09
-
-
Save mwmwmw/d62fdcd57f6c2fddb5e23d23f91f35f9 to your computer and use it in GitHub Desktop.
A React hook for grabbing assets from OpenSea
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 { 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