useViewKey

If adapter supporting fetching view key, useViewKey returns it

Parameters:

interface IUseViewKeyArguments {
    enabled?: boolean // if true auto request view key (default true)
}

Return values:

viewKey: string | null // wallet view key
loading: boolean // displaying loading state
error: any // displaying error message if something went wrong
requestViewKey: () => Promise<void> // function to force request view key

Example (live demo https://aleo-react-boilerplate.vercel.app/useViewKey):

import { FC } from 'react'
import { useViewKey } from 'aleo-hooks'

export const UseViewKey: FC = () => {
    const { viewKey, requestViewKey, error, loading } = useViewKey()

    return (
        <div style={{ display: 'flex', flexDirection: 'column' }}>
            {loading && <p>Loading...</p>}
            {error && <p>Something went wrong {JSON.stringify(error)}</p>}
            {viewKey && <p>View key: {viewKey}</p>}
            <button disabled={loading} onClick={requestViewKey}>Request view key</button>
        </div>
    )
}

Last updated