React
Get started with the Lens React SDK.
The Lens React SDK is a collection of React Hooks for both web and React Native applications. It's the simplest way to interact with the Lens API and build decentralized applications on Lens Protocol.
The Lens React SDK is actively being developed, with new features added regularly. To benefit from the latest improvements and features, we recommend always using the most recent version of the SDK.
Getting Started
To get started, follow the steps below.
Next, define the structure of the data for the key entities in your application by creating GraphQL fragments that customize the data you retrieve.
This step is critical to keeping your queries efficient and focused, helping you avoid overfetching unnecessary data.
See the example below for a few common fragments.
Throughout this documentation, you’ll explore additional fragments and fields that you may want to tailor to your needs.
See the Custom Fragments best-practices guide for more information.
Then, create an index.ts where you extend the TypeScript definitions for the entities you are customizing.
fragments/index.ts
import type { FragmentOf } from "@lens-protocol/react";
import { AccountFragment, AccountMetadataFragment } from "./accounts";import { PostMetadataFragment } from "./posts";import { MediaImageFragment } from "./images";
declare module "@lens-protocol/react" { export interface Account extends FragmentOf<typeof AccountFragment> {} export interface AccountMetadata extends FragmentOf<typeof AccountMetadataFragment> {} export interface MediaImage extends FragmentOf<typeof MediaImageFragment> {} export type PostMetadata = FragmentOf<typeof PostMetadataFragment>;}
export const fragments = [ AccountFragment, PostMetadataFragment, MediaImageFragment,];
Next, create an instance of the PublicClient pointing to the desired environment.
Finally, wrap your app with the <LensProvider> component and pass the PublicClient instance you created in the previous step.
App.tsx
import { LensProvider } from "@lens-protocol/react";
import { client } from "./client";
export function App() { return ( <LensProvider config={client}> {/* Your application components */} </LensProvider> );}
That's it—you're ready to use the Lens React SDK in your app.