This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: examples/browser-create-react-app (#3694)
Co-authored-by: kvutien <kvutien@VTKT5.fritz.box> Co-authored-by: Marcin Rataj <lidel@lidel.org>
- Loading branch information
1 parent
cd548e6
commit dc041aa
Showing
6 changed files
with
111 additions
and
118 deletions.
There are no files selected for viewing
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
File renamed without changes
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import { useState, useEffect } from 'react' | ||
import dotProp from 'dot-prop' | ||
// dot-prop: used to obtain a property of an object when the name of property is a string | ||
// here we get ipfs.id when calling dotProp.get(ipfs, cmd), with cmd = 'id' | ||
// and we get ipfs.hash when calling with cmd = 'hash' etc. | ||
|
||
/* | ||
* Pass the command you'd like to call on an ipfs instance. | ||
* | ||
* Uses setState to capture the response, so your component | ||
* will re-render when the result turns up. | ||
* callIpfs uses setState write the response as a state variable, so that your component | ||
* will re-render when the result 'res' turns up from the call await ipfsCmd. | ||
* | ||
*/ | ||
export default function useIpfs (ipfs, cmd, opts) { | ||
const [res, setRes] = useState(null) | ||
useEffect(() => { | ||
callIpfs(ipfs, cmd, opts, setRes) | ||
}, [ipfs, cmd, opts]) | ||
return res | ||
// note: opts is not used here and is not passed as args of the call from App.js | ||
const [res, setRes] = useState(null) | ||
useEffect(() => { | ||
callIpfs(ipfs, cmd, opts, setRes) | ||
}, [ipfs, cmd, opts]) | ||
return res | ||
} | ||
|
||
async function callIpfs (ipfs, cmd, opts, setRes) { | ||
if (!ipfs) return null | ||
console.log(`Call ipfs.${cmd}`) | ||
const ipfsCmd = dotProp.get(ipfs, cmd) | ||
const res = await ipfsCmd(opts) | ||
console.log(`Result ipfs.${cmd}`, res) | ||
setRes(res) | ||
if (!ipfs) return null | ||
console.log(`Call ipfs.${cmd}`) | ||
const ipfsCmd = dotProp.get(ipfs, cmd) | ||
const res = await ipfsCmd(opts) | ||
console.log(`Result ipfs.${cmd}`, res) | ||
setRes(res) | ||
} |
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