Created
March 17, 2022 13:00
-
-
Save ibelick/b0d06dd5b3ef32f431e930f1bf4b3e78 to your computer and use it in GitHub Desktop.
A simple React hook to upload a file or data into IPFS
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 { 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