React Hooks
hooks
Usesubscription

useSubscription

Hook to create a Nostr subscription

Import

import { useSubscription } from '@lawallet/react';

Usage

index.tsx
import { useSubscription } from '@lawallet/react';
 
function App() {
  const userPubkey: string = '9a9787e3e31c...20f63546b191e28d59';
 
  const { subscription, events } = useSubscription({
    filters: {
      [
        authors: [userPubkey]
        kinds: [1],
      ]
    },
    options: {
      closeOnEose: true
    },
    enabled: true,
    config
  });
}

Parameters

import { type useSubscriptionParameters } from '@lawallet/react';

filters

NDKFilter[]

  • Array of filters for the subscription you want to start
index.tsx
import { useSubscription } from '@lawallet/react';
 
function App() {
  const { subscription, events } = useSubscription({
    filters: {
      [
        authors: ['9a9787e...e28d59']
        kinds: [1],
      ]
    },
    options: {
      closeOnEose: true
    },
    enabled: true
  });
}

options

NDKSubscriptionOptions

  • Options for the subscription you want to start
index.tsx
import { useSubscription } from '@lawallet/react';
import { config } from './config';
 
function App() {
  const { subscription, events } = useSubscription({
    filters: {
      [
        authors: ['9a9787e...e28d59']
        kinds: [1],
      ]
    },
    options: {
      closeOnEose: true
    },
    enabled: true,
    config
  });
}

enabled

Boolean | undefined

  • Set this to false to disable this query from automatically running.
  • The default parameter is true.
index.tsx
import { useSubscription } from '@lawallet/react';
 
function App() {
  const { subscription, events } = useSubscription({
    filters: {
      [
        authors: ['9a9787e...e28d59']
        kinds: [1],
      ]
    },
    options: {
      closeOnEose: true
    },
    enabled: true
  });
}

config

Config | undefined

Config to use instead of retrieving from the from nearest LaWalletConfig.

index.tsx
import { useCards } from '@lawallet/react';
import { config } from './config';
 
function App() {
  const { transactions } =useSubscription({
    filters: {
      [
        authors: ['9a9787e...e28d59']
        kinds: [1],
      ]
    },
    enabled: true,
    config
  });
}

Return Type

import { type useSubscriptionReturns } from '@lawallet/react';

subscription

NDKSubscription

Returns an instance of the subscription

events

NDKEvent[]

Returns an array of events received in the started subscription