If your framework does not support React Server Components, use the useTweet hook within a 'use client' component.
useTweet provides data, error, and isLoading states. You can use these to manage the rendering lifecycle: show the fallback during loading, and show a TweetNotFound component (or a custom one provided via components) if an error occurs or no data is returned. Once loaded, use the EmbeddedTweet component to render the tweet.
'use client'
import {
type TweetProps,
EmbeddedTweet,
TweetNotFound,
TweetSkeleton,
useTweet,
} from 'react-tweet'
export const Tweet = ({
id,
apiUrl,
fallback = <TweetSkeleton />,
components,
onError,
}: TweetProps) => {
const { data, error, isLoading } = useTweet(id, apiUrl)
if (isLoading) return fallback
if (error || !data) {
const NotFound = components?.TweetNotFound || TweetNotFound
return <NotFound error={onError ? onError(error) : error} />
}
return <EmbeddedTweet tweet={data} components={components} />
}