Skip to content

Instantly share code, notes, and snippets.

@ibelick
Created March 17, 2022 13:00
Show Gist options
  • Save ibelick/b0d06dd5b3ef32f431e930f1bf4b3e78 to your computer and use it in GitHub Desktop.
Save ibelick/b0d06dd5b3ef32f431e930f1bf4b3e78 to your computer and use it in GitHub Desktop.
A simple React hook to upload a file or data into IPFS
import { create } from "ipfs-http-client";
import { ImportCandidate } from "ipfs-core-types/src/utils";
const client = create({ url: "https://ipfs.infura.io:5001/api/v0" });
const useIpfs = () => {
const upload = async (file: ImportCandidate) => {
try {
const added = await client.add(file);
const url = `https://ipfs.infura.io/ipfs/${added.path}`;
return url;
} catch (error) {
console.error("Error uploading file: ", error);
}
};
return {
upload,
};
};
export default useIpfs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment