
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:
useNDK
useNDKCurrentUser
useProfile
# 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 | 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 networkuseUserProfilesStore
A 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 75 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.