Usage with React
Introduction
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.
Usage
API
<Connect />
This component is required in order to use the provided hooks.
location
String
The Ethereum address or ENS domain of an Aragon organization.
connector
Connector
or [String, Object]
or String
Accepts a Connector
instance, and either a string or a tuple for embedded connectors and their config.
options
Object
The optional configuration object.
useOrganization()
returns
[Organization | null, { loading: boolean, error: null | Error, retry: Function }]
An array containing the organization and a loading status object.
useApp(appFilters)
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.
returns
[App | null, { loading: boolean, error: null | Error, retry: Function }]
An array containing a single app from the organization and a loading status object.
usePermissions()
returns
[Permission[], { loading: boolean, error: null | Error, retry: Function }]
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:
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.
This is how you can use it:
Subscriptions
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.
Last updated