Skip to content

useDisconnect

The useDisconnect hook provides functionality to disconnect from the currently connected wallet.

Import

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

Usage

Basic Disconnection

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

function DisconnectWallet() {
  const { disconnect, isPending } = useDisconnect()
  const { status } = useConnect()

  if (status !== ConnectionStatus.Connected) {
    return null
  }

  return (
    <button onClick={() => disconnect()} disabled={isPending}>
      {isPending ? 'Disconnecting...' : 'Disconnect'}
    </button>
  )
}
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 Disconnection

tsx
function AsyncDisconnect() {
  const { disconnectAsync, isPending, error } = useDisconnect()

  const handleDisconnect = async () => {
    try {
      await disconnectAsync()
      console.log('Disconnected successfully!')
    } catch (err) {
      console.error('Disconnection failed:', err)
    }
  }

  return (
    <div>
      <button onClick={handleDisconnect} disabled={isPending}>
        {isPending ? 'Disconnecting...' : 'Disconnect Wallet'}
      </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 disconnection succeeds
onError(error: Error) => voidNoCallback when disconnection fails
onSettled() => voidNoCallback when disconnection 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 disconnections.

PropertyTypeDescription
disconnect(options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => voidDisconnect from wallet
disconnectAsync(options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => Promise<void>Async version of disconnect
reset() => voidReset the disconnection state

Data & State

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

PropertyTypeDescription
statusConnectionStatusThe current connection status. Possible values are disconnected, connecting, connected, and disconnecting.
datavoid | undefinedDisconnection result data
errorError | nullDisconnection error if any
isErrorbooleanWhether there's an error
isIdlebooleanWhether the hook is idle
isPendingbooleanWhether disconnection is in progress
isSuccessbooleanWhether disconnection was successful
variablesvoid | undefinedLast used disconnection variables

Released under the MIT License.