Common Scenarios

Learn strategies for handling common scenarios involving Lens Profiles.


Single Profile

Retrieve a specific Lens Profile.

The useProfile and useLazyProfile hooks enable you to retrieve a specific Lens Profile using its Handle or ID.

Available in @lens-protocol/react-web and @lens-protocol/react-native

Fetch a Profile by Handle.

import { useProfile } from '@lens-protocol/react-web';
export function SingleProfile() {  const { data, error, loading } = useProfile({    forHandle: 'lens/brainjammer'  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profile: {error.message}</p>;
  return (    <p>Profile: {data.handle?.fullHandle ?? data.id}</p>  );}

Fetch a Profile by ID.

import { ProfileId, useProfile } from '@lens-protocol/react-web';
export function SingleProfile({ id }: { id: ProfileId }) {  const { data, error, loading } = useProfile({    forProfileId: id  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profile: {error.message}</p>;
  return (    <p>Profile: {data.handle?.fullHandle ?? data.id}</p>  );}

Multiple Profiles

Retrieve multiple Lens Profiles by different criteria.

The useProfiles and useLazyProfiles are two versatile hooks that allow you to retrieve multiple profiles based on different criteria.

Available in @lens-protocol/react-web and @lens-protocol/react-native

Fetch multiple Profiles by their Handles.

import { useProfiles } from '@lens-protocol/react-web';
export function MultipleProfiles() {  const { data, error, loading } = useProfiles({    where: {      handles: ['lens/brainjammer', 'lens/krzysu']    }  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

Fetch multiple Profiles by their IDs.

import { profileId, useProfiles } from '@lens-protocol/react-web';
export function MultipleProfiles() {  const { data, error, loading } = useProfiles({    where: {      profileIds: [profileId('0x01'), profileId('0x02')]    }  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

Fetch multiple Profiles by their owner addresses.

import { useProfiles } from '@lens-protocol/react-web';
export function MultipleProfiles() {  const { data, error, loading } = useProfiles({    where: {      ownedBy: [        '0xe3D871d389BF78c091E29deCe83200E9d6B2B0C2',        '0x7c5d4F8345e66f68099581Db340cd65B078C41f4',      ],    },  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

Fetch all Profiles who have mirrored a specific publication.

import { publicationId, useProfiles } from '@lens-protocol/react-web';
export function ProfilesThatMirroredPublication() {  const { data, loading, error } = useProfiles({    where: {      whoMirroredPublication: publicationId('0x0167-0x56'),    },  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  if (data.length === 0) return <p>No profiles have mirrored this publication.</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

Fetch all Profiles who have commented on a specific publication.

import { publicationId, useProfiles } from '@lens-protocol/react-web';
export function ProfilesThatMirroredPublication() {  const { data, loading, error } = useProfiles({    where: {      whoCommentedOn: publicationId('0x0167-0x56'),    },  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  if (data.length === 0) return <p>No profiles have commented on this publication.</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

Fetch all Profiles who have quoted a specific publication.

import { publicationId, useProfiles } from '@lens-protocol/react-web';
export function ProfilesThatMirroredPublication() {  const { data, loading, error } = useProfiles({    where: {      whoQuotedPublication: publicationId('0x0167-0x56'),    },  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching profiles: {error.message}</p>;
  if (data.length === 0) return <p>No profiles have quoted this publication.</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

The useProfiles hook yields a PaginatedReadResult<Profile[]>. For more information on pagination, refer to this guide.

Collectors

Retrieve all Profiles that have collected a specific publication.

You can use the useWhoActedOnPublication hook for this purpose.

Available in @lens-protocol/react-web and @lens-protocol/react-native

import {  useWhoActedOnPublication,  OpenActionCategoryType,  publicationId,} from '@lens-protocol/react-web';
export function PublicationCollectors() {  const { data, loading, error } = useWhoActedOnPublication({    on: publicationId('0x56-0x02'),    where: {      anyOf: [        {          category: OpenActionCategoryType.Collect,        },      ],    },  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching publication collectors: {error.message}</p>;
  if (data.length === 0) return <p>Nobody has collected this publication.</p>;
  return (    <ul>      {data.map((profile) => (        <li key={profile.id}>{profile.handle?.fullHandle ?? profile.id}</li>      ))}    </ul>  );}

The hook returns PaginatedReadResult<Profile[]>. For more information on pagination, refer to this guide.

Reaction Insight

Retrieve all Profiles that have reacted to a specific publication.

You can use the useWhoReactedToPublication hook for this purpose.

Available in @lens-protocol/react-web and @lens-protocol/react-native

import { publicationId, useWhoReactedToPublication } from '@lens-protocol/react-web';
export function PublicationReactions() {  const { data, loading, error } = useWhoReactedToPublication({    for: publicationId('0x56-0x02'),  });
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error fetching who reacted to publication: {error.message}</p>;
  if (data.length === 0) return <p>No profiles have reacted to this publication.</p>;
  return (    <ul>      {data.map((profileWhoReactedResult) => (        <li key={profileWhoReactedResult.profile.id}>          <span>            Profile:            {profileWhoReactedResult.profile.handle?.fullHandle ??              profileWhoReactedResult.profile.id}          </span>          <div>            Reactions:            <ul>              {profileWhoReactedResult.reactions.map((reaction) => (                <li key={reaction.reaction}>                  {reaction.reaction} at {reaction.reactionAt}                </li>              ))}            </ul>          </div>        </li>      ))}    </ul>  );}

The hook returns PaginatedReadResult<ProfileWhoReactedResult[]>. For more information on pagination, refer to this guide.


That's it—you now have a solid understanding of how to retrieve Lens Profiles in typical social app scenarios.