diff --git a/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js b/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js index 9147b4678d..dcd4da6607 100644 --- a/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js +++ b/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js @@ -13,7 +13,7 @@ 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) @@ -21,6 +21,27 @@ export default function useIpfsFactory ({ commands }) { // 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) { @@ -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 } }