editing my last post as I managed to fix this.
TLDR; itās all about autoFetch
option in useMoralisWeb3ApiCall
hook
when I added { autoFetch: !!token && addr !== "explore" },
it started to work
the original code that did not work (nft details were not fetched):
export const useNFTTokenIds = (addr) => {
const { token } = useMoralisWeb3Api();
const { chainId } = useMoralisDapp();
const { resolveLink } = useIPFS();
const [NFTTokenIds, setNFTTokenIds] = useState([]);
const [totalNFTs, setTotalNFTs] = useState();
const [fetchSuccess, setFetchSuccess] = useState(true);
const {
fetch: getNFTTokenIds,
data,
error,
isLoading,
} = useMoralisWeb3ApiCall(token.getAllTokenIds, {
chain: chainId,
address: addr,
limit: 10,
});
console.log('useNFTTokenIds', data)
useEffect(async () => {
console.log('useEffect useNFTTokenIds', data?.result)
if (data?.result) {
console.log('useEffect useNFTTokenIds | retrived data')
const NFTs = data.result;
setTotalNFTs(data.total);
setFetchSuccess(true);
for (let NFT of NFTs) {
if (NFT?.metadata) {
NFT.metadata = JSON.parse(NFT.metadata);
NFT.image = resolveLink(NFT.metadata?.image);
} else if (NFT?.token_uri) {
try {
await fetch(NFT.token_uri)
.then((response) => response.json())
.then((data) => {
NFT.image = resolveLink(data.image);
});
} catch (error) {
setFetchSuccess(false);
}
}
}
setNFTTokenIds(NFTs);
}
}, [data]);
return {
getNFTTokenIds,
NFTTokenIds,
totalNFTs,
fetchSuccess,
error,
isLoading,
};
};
new code that works
export const useNFTTokenIds = (addr, limit = 3) => {
const { token } = useMoralisWeb3Api();
const { chainId } = useMoralis();
const { resolveLink } = useIPFS();
const getAllTokenIdsOpts = {
chain: chainId,
address: addr,
limit: limit,
};
const {
fetch: getNFTTokenIds,
data,
error,
isLoading,
isFetching,
} = useMoralisWeb3ApiCall(
token.getAllTokenIds,
getAllTokenIdsOpts,
{ autoFetch: !!token && addr !== "explore" },
);
const NFTTokenIds = useMemo(() => {
console.log('fetching tokenIds data')
if (!data?.result || !data?.result.length) {
return data;
}
const formattedResult = data.result.map((nft) => {
try {
if (nft.metadata) {
const metadata = JSON.parse(nft.metadata);
const image = resolveLink(metadata?.image);
return { ...nft, image, metadata };
}
} catch (error) {
return nft;
}
return nft;
});
return { ...data, result: formattedResult };
}, [data]);
return { getNFTTokenIds, data: NFTTokenIds, error, isLoading, isFetching };
};