
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning 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:
useNDKuseNDKCurrentUseruseProfile# npm
npm install @nostr-dev-kit/ndk-hooks
# pnpm
pnpm add @nostr-dev-kit/ndk-hooks
# yarn
yarn add @nostr-dev-kit/ndk-hooks
First, initialize the NDK instance and make it available to your components:
import NDK from '@nostr-dev-kit/ndk';
import { useNDK } from '@nostr-dev-kit/ndk-hooks';
function App() {
const { setNDK } = useNDK();
useEffect(() => {
const ndk = new NDK({
explicitRelayUrls: ['wss://relay.nostr.band', 'wss://relay.damus.io'],
});
ndk.connect().then(() => {
setNDK(ndk);
});
}, [setNDK]);
return <YourApp />;
}
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>
{/* Display user information */}
</div>
);
}
For more details on NDK store and hooks, see the NDK Hooks Documentation.
Before using the profile hooks, you need to initialize the profiles store with your NDK instance:
import NDK from '@nostr-dev-kit/ndk';
import { useUserProfilesStore } from '@nostr-dev-kit/ndk-hooks';
// During app initialization
const ndk = new NDK({
explicitRelayUrls: ['wss://relay.nostr.band', 'wss://relay.damus.io'],
});
await ndk.connect();
// Initialize the profiles store
useUserProfilesStore.getState().initialize(ndk);
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);
You can directly interact with the underlying Zustand store:
import { useUserProfilesStore } from '@nostr-dev-kit/ndk-hooks';
// Set a profile manually
useUserProfilesStore.getState().setProfile(pubkey, profile);
// Fetch a profile manually
useUserProfilesStore.getState().fetchProfile(pubkey);
// Get all profiles
const profiles = useUserProfilesStore.getState().profiles;
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 networkuseUserProfilesStoreA Zustand store that manages user profiles.
Properties:
profiles - Map of pubkeys to profile objectslastFetchedAt - Map of pubkeys to timestamps of last fetchndk - The NDK instanceMethods:
initialize(ndk: NDK) - Initialize the store with an NDK instancesetProfile(pubkey: string, profile: NDKUserProfile, cachedAt?: number) - Manually set a profilefetchProfile(pubkey?: string, force?: boolean) - Fetch a profile from the networkMIT
FAQs
React hooks for the Nostr Development Kit (NDK)
The npm package @nostr-dev-kit/ndk-hooks receives a total of 392 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
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.