React Hooks
hooks
Usenostr

useNostr

Hook to use connections to Nostr

Import

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

Usage

index.tsx
import { useNostr } from '@lawallet/react';
 
function App() {
  const { ndk, connectNDK } = useNostr({
    explicitRelayUrls: ['wss://relay.lawallet.ar'],
  });
}

Parameters

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

explicitRelayUrls

String[]

  • Array of relay links to connect
import { useTransactions } from '@lawallet/react';
 
function App() {
  const { ndk, connectNDK } = useNostr({
    explicitRelayUrls: ['wss://relay.lawallet.ar'], // [!code focus]
  });
}

explicitSigner

SignerTypes | undefined

  • Initialize an explicit signer to sign events
import { useTransactions, createSignerWithPrivateKey } from '@lawallet/react';
 
const signer = createSignerWithPrivateKey('5caa3cd87cf1ad069bc...7b6070a44ec7223877504c84'); // [!code focus]
 
function App() {
  const { ndk, connectNDK } = useNostr({
    explicitRelayUrls: ['wss://relay.lawallet.ar'],
    explicitSigner: signer, // [!code focus]
  });
}

autoConnect

Boolean | undefined

  • Set this to false to disable automatic connection of relays.
  • The default parameter is true.
import { useNostr } from '@lawallet/react';
 
function App() {
  const { ndk, connectNDK } = useNostr({
    explicitRelayUrls: ['wss://relay.lawallet.ar'],
    autoConnect: false // [!code focus]
  });
 
  useEffect(() = {
    connectNDK()
  }, [])
}

Return Type

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

ndk

NDK

Returns an instance of the NDK

signer

Returns an instance of the NDKSigner

signerInfo

Returns an instance of the NDKUser

providers

LightningProvidersType

Returns the window.ln and window.nostr browser instances

connectNDK

() => Promise<boolean>

  • This function executes the connection to the relays
  • Returns a boolean variable that validates whether it was executed correctly

initializeSigner

(signer: SignerTypes): void <SignerTypes>

  • This function sets the signing user of the NDK instance.
  • Use UseSigner to access it if you are within the LaWalletConfig context

authWithPrivateKey

(hex: string): Promise<SignerTypes> <SignerTypes>

  • This function authenticates a signer with a private key
  • Returns an instance of NDKSigner

authWithExtension

(): Promise<SignerTypes> <SignerTypes>