Skip to content

useConnect

The useConnect hook manages wallet connections, providing methods to connect to wallets and access connection state.

Import

tsx
import { useConnect } from '@luno-kit/react'
import type { ConnectionStatus } from '@luno-kit/react'

Usage

Basic Connection

tsx
import { useConnect } from '@luno-kit/react'
import type { ConnectionStatus } from '@luno-kit/react'

function ConnectWallet() {
  const { connect, connectors, status } = useConnect()
  
  const handleConnect = (connectorId: string) => {
    connect({ connectorId })
  }
  
  if (status === ConnectionStatus.Connected) {
    return <div>✅ Connected</div>
  }
  
  return (
    <div>
      <h3>Choose a wallet:</h3>
      {connectors.map((connector) => (
        <button
          key={connector.id}
          onClick={() => handleConnect(connector.id)}
          disabled={isPending}
        >
          <img src={connector.icon} alt={connector.name} width={24} height={24} />
          {connector.name}
        </button>
      ))}
    </div>
  )
}
ts
import { createConfig, kusama, polkadot, polkadotjsConnector } from '@luno-kit/react'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Async Connection

tsx
function AsyncConnect() {
  const { connectAsync, isPending, error } = useConnect()
  
  const handleConnect = async () => {
    try {
      await connectAsync({ connectorId: 'polkadot-js' })
      console.log('Connected successfully!')
    } catch (err) {
      console.error('Connection failed:', err)
    }
  }
  
  return (
    <div>
      <button onClick={handleConnect} disabled={isPending}>
        {isPending ? 'Connecting...' : 'Connect Polkadot{.js}'}
      </button>
      {error && <p>Error: {error.message}</p>}
    </div>
  )
}
ts
import { createConfig, kusama, polkadot, polkadotjsConnector } from '@luno-kit/react'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Parameters

The hook accepts an optional configuration object:

ParameterTypeRequiredDescription
onSuccess() => voidNoCallback when connection succeeds
onError(error: Error) => voidNoCallback when connection fails
onSettled() => voidNoCallback when connection attempt completes

Return Value

The hook returns an object with the following properties:

Methods

These are the functions you can call to initiate and manage connections.

PropertyTypeDescription
connect(variables: { connectorId: string; targetChainId?: string; }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => voidConnect to a wallet
connectAsync(variables: { connectorId: string; targetChainId?: string; }, options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => void) => Promise<void>Async version of connect
reset() => voidReset the connection state

Data & State

These properties provide information about the current state of the connection.

PropertyTypeDescription
connectorsConnector[]Array of available wallet connectors
activeConnectorConnector | undefinedCurrently active connector
statusConnectionStatusThe current connection status. Possible values are disconnected, connecting, connected, and disconnecting.
datavoid | undefinedConnection result data
variables{ connectorId: string; targetChainId?: string; } | undefinedLast used connection variables
errorError | nullConnection error if any
isErrorbooleanWhether there's an error
isIdlebooleanWhether the hook is idle
isPendingbooleanWhether connection is in progress
isSuccessbooleanWhether connection was successful

Released under the MIT License.