
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@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 useNDK
useNDKCurrentUser
useProfileValue
useNDKSessions
# 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 useProfileValue 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 useProfileValue
hook to fetch and display user profiles:
import { useProfileValue } from '@nostr-dev-kit/ndk-hooks';
function UserCard({ pubkey }) {
const profile = useProfileValue(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 = useProfileValue(pubkey, true);
useNDKInit(): (ndkInstance: NDK) => void
Provides 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 useruseProfileValue(pubkey: string, forceRefresh?: boolean): NDKUserProfile | undefined
Fetches 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 networkuseNDKSessions
A 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 | undefined
Returns 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 49 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.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.