Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frame-react package #35

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add frame-react package #35

wants to merge 4 commits into from

Conversation

gskril
Copy link

@gskril gskril commented Dec 14, 2024

This package makes loading the frame-sdk and fetching data from its context easier. It can probably be implemented directly into the frame-sdk package, but I followed the repo's pattern of making a new one.

Basic implementation in a Next.js app (assumes the dev has already added the custom connector to wagmiConfig):

// Providers.tsx
'use client'

import { FrameProvider } from '@farcaster/frame-react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { WagmiProvider } from 'wagmi'

import { wagmiConfig } from '@/lib/web3'

const queryClient = new QueryClient()

export function ClientProviders({ children }: { children: React.ReactNode }) {
  return (
    <WagmiProvider config={wagmiConfig}>
      <QueryClientProvider client={queryClient}>
        <FrameProvider>{children}</FrameProvider>
      </QueryClientProvider>
    </WagmiProvider>
  )
}

Wrap the entire app (layout.tsx), or just the frame routes, in our new provider:

import type { Metadata } from 'next';

import '@/app/globals.css';
import { ClientProviders } from '@/components/ClientProviders';

export const metadata: Metadata = {
  title: 'Farcaster Frames v2 Demo',
  description: 'A Farcaster Frames v2 demo app',
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body>
        <ClientProviders>{children}</ClientProviders>
      </body>
    </html>
  );
}

Then from any client component:

// page.tsx
'use client'

import { useFrame } from '@farcaster/frame-react'

export default function Page() {
  const frameContext = useFrame()

  return <div>Hello {frameContext?.user.username}</div>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant