React Tweet Embeds
react-tweet is a library maintained by Vercel that enables seamless embedding of individual tweets into React applications, supporting various build tools like Next.js and Vite. It provides components for rendering tweets, including media, and utilities for fetching tweet data. The current stable version is 3.3.0. The package maintains an active release cadence, frequently pushing minor updates and bug fixes, often in response to changes in the underlying X (formerly Twitter) platform's API or styling. Key differentiators include its tight integration with the Vercel ecosystem, built-in styling for a consistent look, and a focus on providing a reliable way to display tweet content that often faces challenges due to Twitter's evolving API access and rate limits.
Common errors
-
Module not found: Can't resolve 'react-tweet'
cause Incorrect import path for components or API functions after v3.0.0.fixChange `import { Tweet } from 'react-tweet';` to `import { Tweet } from 'react-tweet/components';` or `import { getTweet } from 'react-tweet/api';`. -
Error: Children of Server Components cannot have a `key` prop. Remove the `key` prop from your `Tweet` component.
cause Using `key` prop directly on a `Tweet` component that is being rendered within a Server Component context.fixIf `Tweet` is a Server Component, the `key` prop should be applied to the parent element if mapping over a list, not directly to `Tweet` itself. -
TypeError: Cannot read properties of undefined (reading 'media_details')
cause `rawTweet` prop passed to `Tweet` component is `undefined` or malformed, often because `getTweet` returned `undefined`.fixAlways verify that `getTweet` returns a valid tweet object before passing it to the `Tweet` component. Add checks like `if (!tweet) return null;`. -
Tweet media buttons still show 'Twitter' logo instead of 'X'.
cause Outdated version of `react-tweet` or missing styles, causing older assets to be displayed.fixUpdate to `react-tweet@3.1.0` or newer. Ensure styles are correctly imported, as `react-tweet/styles` provides the necessary icons.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, specifically around import paths. Components like `Tweet` and `TweetSkeleton` are now imported from `react-tweet/components`, and API utilities like `getTweet` are from `react-tweet/api`.
- gotcha Styling for react-tweet is often managed by importing a CSS file. If styles are missing, tweets may appear unformatted.
- breaking The `date-fns` dependency was removed in v3.2.1, and the time component was refactored to be a server component. This might affect client-side rendering of tweet timestamps if your setup relied on client-side `date-fns` formatting.
- gotcha When `getTweet` encounters a private or non-existent tweet, it will log to the console and return `undefined`. Your application must handle this case gracefully.
- breaking React 19 was added to peer dependencies in v3.2.2. While this primarily indicates compatibility, older versions of React might show peer dependency warnings.
Install
-
npm install react-tweet -
yarn add react-tweet -
pnpm add react-tweet
Imports
- Tweet
import { Tweet } from 'react-tweet';import { Tweet } from 'react-tweet/components'; - getTweet
import { getTweet } from 'react-tweet';import { getTweet } from 'react-tweet/api'; - TweetSkeleton
const { TweetSkeleton } = require('react-tweet');import { TweetSkeleton } from 'react-tweet/components';
Quickstart
import { Tweet } from 'react-tweet/components';
import { getTweet } from 'react-tweet/api';
// This component is designed for server-side fetching (e.g., Next.js Server Components)
// or client-side fetching with appropriate state management.
async function MyTweetEmbed({ id }: { id: string }) {
const tweet = await getTweet(id);
if (!tweet) {
return <p>Tweet not found or private.</p>;
}
return (
<div style={{
display: 'flex',
justifyContent: 'center',
width: '100%',
margin: '20px 0'
}}>
<Tweet rawTweet={tweet} />
</div>
);
}
// Example usage in a React component:
// <MyTweetEmbed id="1713028308833912089" />
// Remember to call `getTweet` in a server component or `useEffect` for client components.