
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@nostr-dev-kit/ndk-hooks
Advanced tools
React hooks for the Nostr Development Kit (NDK)
@nostr-dev-kit/ndk-hooks provides a set of React hooks and utilities to easily integrate Nostr functionality into your React applications using NDK. This library helps you efficiently manage Nostr data in your React components, including:
useNDKInit and useNDKuseNDKCurrentUseruseProfileuseNDKSessions# npm
npm install @nostr-dev-kit/ndk-hooks
# pnpm
pnpm add @nostr-dev-kit/ndk-hooks
# yarn
yarn add @nostr-dev-kit/ndk-hooks
The library provides a centralized way to initialize the NDK instance across all stores using the useNDKInit hook:
import NDK from '@nostr-dev-kit/ndk';
import { useNDKInit } from '@nostr-dev-kit/ndk-hooks';
const ndk = new NDK({
explicitRelayUrls: ['wss://relay.nostr.band', 'wss://relay.damus.io'],
});
ndk.connect();
function App() {
const initializeNDK = useNDKInit();
useEffect(() => {
const setupNDK = async () => {
initializeNDK(ndk);
// This initializes the NDK instance in all stores:
// - NDK store (for useNDK hook)
// - Session store (for useNDKSessions hook)
// - Profile store (for useProfile hook)
};
setupNDK();
}, [initializeNDK]);
return <YourApp />;
}
Once initialized, you can access the NDK instance with the useNDK hook in your components.
You can access and set the current user with useNDKCurrentUser:
import { useNDKCurrentUser } from '@nostr-dev-kit/ndk-hooks';
function UserProfile() {
const { currentUser } = useNDKCurrentUser();
if (!currentUser) {
return <div>Not logged in</div>;
}
return (
<div>
<h2>Logged in as: {currentUser.pubkey}</h2>
</div>
);
}
For more details on NDK store and hooks, see the NDK Hooks Documentation.
The profile management functionality is automatically initialized when you use the useNDKInit hook as shown above. You don't need to do any additional setup.
Use the useProfile hook to fetch and display user profiles:
import { useProfile } from '@nostr-dev-kit/ndk-hooks';
function UserCard({ pubkey }) {
const profile = useProfile(pubkey);
if (!profile) {
return <div>Loading profile...</div>;
}
return (
<div>
<img src={profile.picture} alt={profile.name} />
<h2>{profile.name || 'Anonymous'}</h2>
<p>{profile.about}</p>
</div>
);
}
You can force a profile to be refreshed from the network by passing true as the second parameter:
// This will fetch the profile from the network even if it's cached
const profile = useProfile(pubkey, true);
useNDKInit(): (ndkInstance: NDK) => voidProvides a function to initialize the NDK instance across all stores in the application.
useNDK(): { ndk: NDK | null, setNDK: (ndk: NDK) => void }Provides access to the NDK instance and a function to set it.
ndk - The current NDK instance or null if not setsetNDK - Function to set the NDK instanceuseNDKCurrentUser(): { currentUser: NDKUser | null, setCurrentUser: (user: NDKUser | null) => void }Provides access to the current user and a function to set it.
currentUser - The current user or null if not logged insetCurrentUser - Function to set the current useruseProfile(pubkey: string, forceRefresh?: boolean): NDKUserProfile | undefinedFetches and returns a Nostr user profile for the given pubkey.
pubkey - The hex pubkey of the userforceRefresh - (Optional) Whether to force a refresh of the profile from the networkuseNDKSessionsA Zustand store that manages user sessions. The store now manages its own NDK instance internally.
Properties:
ndk - The NDK instance used by the session storesessions - Map of pubkeys to session dataactiveSessionPubkey - The pubkey of the active sessionPublic Methods:
initSession(user: NDKUser, signer?: NDKSigner, opts?: SessionInitOptions) - Initialize a session for a userdeleteSession(pubkey: string) - Delete a sessionsetActiveSession(pubkey: string | null) - Set the active sessionmuteItemForSession(pubkey: string, value: string, itemType: string, publish?: boolean) - Mute an item for a sessionuseUserSession(pubkey?: string): UserSessionData | undefinedReturns the session data for the specified pubkey or the active session if no pubkey is provided.
For more details on session management, see the Session Management Documentation.
MIT
FAQs
React hooks for the Nostr Development Kit (NDK)
The npm package @nostr-dev-kit/ndk-hooks receives a total of 217 weekly downloads. As such, @nostr-dev-kit/ndk-hooks popularity was classified as not popular.
We found that @nostr-dev-kit/ndk-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.