Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: fix react example (#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored Apr 28, 2020
1 parent c16b5b4 commit 2cf6b49
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions examples/browser-create-react-app/src/hooks/use-ipfs-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,35 @@ let ipfs = null
* so use-ipfs calls can grab it from there rather than expecting
* it to be passed in.
*/
export default function useIpfsFactory ({ commands }) {
export default function useIpfsFactory () {
const [isIpfsReady, setIpfsReady] = useState(Boolean(ipfs))
const [ipfsInitError, setIpfsInitError] = useState(null)

useEffect(() => {
// The fn to useEffect should not return anything other than a cleanup fn,
// So it cannot be marked async, which causes it to return a promise,
// Hence we delegate to a async fn rather than making the param an async fn.
async function startIpfs () {
if (ipfs) {
console.log('IPFS already started')
} else if (window.ipfs && window.ipfs.enable) {
console.log('Found window.ipfs')
ipfs = await window.ipfs.enable({ commands: ['id'] })
} else {
try {
console.time('IPFS Started')
ipfs = await Ipfs.create()
console.timeEnd('IPFS Started')
} catch (error) {
console.error('IPFS init error:', error)
ipfs = null
setIpfsInitError(error)
}
}

setIpfsReady(Boolean(ipfs))
}

startIpfs()
return function cleanup () {
if (ipfs && ipfs.stop) {
Expand All @@ -30,28 +51,7 @@ export default function useIpfsFactory ({ commands }) {
setIpfsReady(false)
}
}
})

async function startIpfs () {
if (ipfs) {
console.log('IPFS already started')
} else if (window.ipfs && window.ipfs.enable) {
console.log('Found window.ipfs')
ipfs = await window.ipfs.enable({ commands })
} else {
try {
console.time('IPFS Started')
ipfs = await Ipfs.create()
console.timeEnd('IPFS Started')
} catch (error) {
console.error('IPFS init error:', error)
ipfs = null
setIpfsInitError(error)
}
}

setIpfsReady(Boolean(ipfs))
}
}, [])

return { ipfs, isIpfsReady, ipfsInitError }
}

0 comments on commit 2cf6b49

Please sign in to comment.