Aragon Connect provides a series of utilities that simplify the usage of Aragon Connect in a React environment.
It consists of the <Connect /> component, through which a connection to an organization is described, and a series of hooks: useApp(), useApps(), useOrganization(), usePermissions().
To get started, add the @aragon/connect-react package to your project. It contains all the exports of the @aragon/connect, so you don’t have to install both.
An array containing the organization and a loading status object.
useApp(appFilters)
Name
Type
Description
appFilter
String or Object (optional)
When a string is passed, the app will get searched by address if it starts by 0x, and by appName otherwise. See appFilter.address and appFilter.appName to set them explicitly. For the time being, only one type of filter can get passed at a time.
appFilter.address
String
Same as appFilter, but makes the selection by address explicit.
appFilter.appName
String
Same as appFilter, but makes the selection by appName explicit.
An array containing the organization permissions and a loading status object.
createAppHook()
This utility function makes app connectors available in your React app.
This is how it works at the most basic level:
import { createAppHook, useApp } from'@aragon/connect-react'import connectVoting from'@aragon/connect-voting'// We create a hook corresponding to the app connector. This is usually enough,// since the app connector will inherit from the connection set on <Connect />.constuseVoting=createAppHook(connectVoting)functionVotes() {const [voting] =useApp('voting')// And this is how we can use it, by passing the app instance and a callback.const [votes] =useVoting(voting, (app) =>app.votes())return ( <ul> {votes ? (votes.map((vote) => <likey={vote.id}>{formatVote(vote)}</li>) ) : ( <li>Loading votes…</li> )} </ul> )}
Dependency array
By default, the callback will be called once, and never update afterwards. This can be a problem if you want to reload data depending on the current state. This is why the hook also accept a dependency parameter. It behaves in a very similar way to the useEffect() or useMemo() hooks, except that it doesn’t update the callback when omitted.
An issue with the previous examples is that we only fetch the data once, instead of receiving updates from it. For example, someone might create a new vote, and it is reasonable to expect an app to reflect that. With the app connectors API, you generally have onX equivalents of the async methods, like votes(filters) and onVotes(filters, callback).
Using them with createAppHook() hooks requires to call the onX equivalent of the async method you want to use, but without passing a callback. App connectors return a partially applied function when the callback is omitted, which createAppHook() takes advantage of by entirely managing the subscription.
import connect from'@aragon/connect'import connectVoting from'@aragon/connect-voting'import { createAppHook, useApp } from'@aragon/connect-react'constuseVoting=createAppHook(connectVoting)functionApp() {const [page,setPage] =useState(0)const [voting] =useApp('voting')// votes will now get updated automaticallyconst [votes] =useVoting( voting,// Note that we are now using onVotes() rather than votes() (app) =>app.onVotes({ first:10, skip:10* page }),// When page changes, a new subscription will replace the previous one [page] )return ( <div> <ul> {votes ? (votes.map((vote) => <likey={vote.id}>{formatVote(vote)}</li>) ) : ( <li>Loading votes…</li> )} </ul> <buttononClick={() =>setPage(page +1)}>prev</button> <buttononClick={() =>setPage(page -1)}>next</button> </div> )}