Skip to content

Latest commit

 

History

History
132 lines (93 loc) · 4.38 KB

readme.md

File metadata and controls

132 lines (93 loc) · 4.38 KB

react-tweet for Next.js

Installation

Next.js 13.2.1 or higher is required in order to use react-tweet.

Follow the installation docs in the main README.

Usage

In any component, import Tweet from react-tweet and use it like so:

import { Tweet } from 'react-tweet'

export default function Page() {
  return <Tweet id="1628832338187636740" />
}

To see the code in action go to: /apps/next-app/app/light/[tweet]/page.tsx.

Tweet works differently depending on where it's used. If it's used in the App Router it will fetch the tweet in the server. If it's used in the pages directory it will fetch the tweet in the client with SWR.

You can learn more about react-tweet in the API Reference.

Troubleshooting

If you see an error saying that CSS can't be imported from node_modules in the pages directory. Add the following config to next.config.js:

transpilePackages: ['react-tweet']

The error won't happen if the App Router is enabled, where Next.js supports CSS imports from node_modules and it also applies for the pages directory.

Advanced usage

Manual data fetching

You can use the getTweet function from react-tweet/api to fetch the tweet manually. This is useful for SSG pages and for other Next.js data fetching methods.

For example, using getStaticProps in pages/[tweet].tsx to fetch the tweet and send it as props to the page component:

import { useRouter } from 'next/router'
import { getTweet, type Tweet } from 'react-tweet/api'
import { EmbeddedTweet, TweetSkeleton } from 'react-tweet'

export async function getStaticProps({
  params,
}: {
  params: { tweet: string }
}) {
  const tweetId = params.tweet

  try {
    const tweet = await getTweet(tweetId)
    return tweet ? { props: { tweet } } : { notFound: true }
  } catch (error) {
    return { notFound: true }
  }
}

export async function getStaticPaths() {
  return { paths: [], fallback: true }
}

export default function Page({ tweet }: { tweet: Tweet }) {
  const { isFallback } = useRouter()
  return isFallback ? <TweetSkeleton /> : <EmbeddedTweet tweet={tweet} />
}

To see the code in action go to: /apps/next-app/pages/dark/[tweet].tsx.

Adding next/image

Add the domain URLs from Twitter to images.remotePatterns in next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  images: {
    remotePatterns: [
      { protocol: 'https', hostname: 'pbs.twimg.com' },
      { protocol: 'https', hostname: 'abs.twimg.com' },
    ],
  },
}

In tweet-components.tsx or elsewhere, import the Image component from next/image and use it to define custom image components for the tweet:

import Image from 'next/image'
import type { TweetComponents } from 'react-tweet'

export const components: TweetComponents = {
  AvatarImg: (props) => <Image {...props} />,
  MediaImg: (props) => <Image {...props} fill unoptimized />,
}

Then pass the components prop to Tweet:

import { Tweet } from 'react-tweet'
import { components } from './tweet-components'

export default function Page() {
  return <Tweet id="1628832338187636740" components={components} />
}

Running the test app

Clone this repository and run the following commands:

pnpm install && pnpm dev --filter=next-app...

The app shows the usage of react-tweet in different scenarios:

react-tweet is imported from the root directory, so you can make changes to the package and see the changes reflected in the app immediately.