Skip to content

Commit 21d9e7e

Browse files
authored
fix: fix react example (ipfs#3011)
1 parent 199aa8e commit 21d9e7e

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/hooks/use-ipfs-factory.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,35 @@ let ipfs = null
1313
* so use-ipfs calls can grab it from there rather than expecting
1414
* it to be passed in.
1515
*/
16-
export default function useIpfsFactory ({ commands }) {
16+
export default function useIpfsFactory () {
1717
const [isIpfsReady, setIpfsReady] = useState(Boolean(ipfs))
1818
const [ipfsInitError, setIpfsInitError] = useState(null)
1919

2020
useEffect(() => {
2121
// The fn to useEffect should not return anything other than a cleanup fn,
2222
// So it cannot be marked async, which causes it to return a promise,
2323
// Hence we delegate to a async fn rather than making the param an async fn.
24+
async function startIpfs () {
25+
if (ipfs) {
26+
console.log('IPFS already started')
27+
} else if (window.ipfs && window.ipfs.enable) {
28+
console.log('Found window.ipfs')
29+
ipfs = await window.ipfs.enable({ commands: ['id'] })
30+
} else {
31+
try {
32+
console.time('IPFS Started')
33+
ipfs = await Ipfs.create()
34+
console.timeEnd('IPFS Started')
35+
} catch (error) {
36+
console.error('IPFS init error:', error)
37+
ipfs = null
38+
setIpfsInitError(error)
39+
}
40+
}
41+
42+
setIpfsReady(Boolean(ipfs))
43+
}
44+
2445
startIpfs()
2546
return function cleanup () {
2647
if (ipfs && ipfs.stop) {
@@ -30,28 +51,7 @@ export default function useIpfsFactory ({ commands }) {
3051
setIpfsReady(false)
3152
}
3253
}
33-
})
34-
35-
async function startIpfs () {
36-
if (ipfs) {
37-
console.log('IPFS already started')
38-
} else if (window.ipfs && window.ipfs.enable) {
39-
console.log('Found window.ipfs')
40-
ipfs = await window.ipfs.enable({ commands })
41-
} else {
42-
try {
43-
console.time('IPFS Started')
44-
ipfs = await Ipfs.create()
45-
console.timeEnd('IPFS Started')
46-
} catch (error) {
47-
console.error('IPFS init error:', error)
48-
ipfs = null
49-
setIpfsInitError(error)
50-
}
51-
}
52-
53-
setIpfsReady(Boolean(ipfs))
54-
}
54+
}, [])
5555

5656
return { ipfs, isIpfsReady, ipfsInitError }
5757
}

0 commit comments

Comments
 (0)