How to Create a TSX HOC next-seo component
2 min readJan 27, 2023
Here is an example of a Higher Order Component (HOC) written in TypeScript and using JSX (TSX) that uses the next-seo library to set SEO information for a page:
Interface file
export type ArticleProps = {
title?: string;
titleTemplate?: string;
noindex?: boolean;
nofollow?: boolean;
robotsProps?: AdditionalRobotsProps;
description?: string;
canonical?: string;
mobileAlternate?: {
media: string;
href: string;
};
languageAlternates?: {
hrefLang: string;
href: string;
}[];
openGraph?: OpenGraph;
facebook?: {
appId: string;
};
twitter?: Twitter;
additionalMetaTags?: ReadonlyArray<MetaTag>;
additionalLinkTags?: {
rel: string;
href: string;
sizes?: string;
media?: string;
type?: string;
color?: string;
keyOverride?: string;
as?: string;
crossOrigin?: string;
}[];
useAppDir?: boolean;
ads?: {
id?: string;
slogan?: string;
canonical?: string;
}[];
views?: number;
loves?: number;
clickes?: number;
comments?: number;
children?: never;
};
export type WithSeoProps = {
[x: string]: string;
}
```HOC code
import { NextSeo } from 'next-seo'
import { FunctionComponent } from 'react'
import { ArticleProps, WithSeoProps } from '../components/Utils/Intf'
const withSeo = (WrappedComponent: FunctionComponent<WithSeoProps>, seoProps: ArticleProps) => {
const WithSeo: FunctionComponent<WithSeoProps> = (props) => (
<>
<NextSeo {…seoProps} />
<WrappedComponent {…props} />
</>
)
return WithSeo
}
export default withSeo
```Get As Md Habibullah’s stories in your inbox
Join Medium for free to get updates from this writer.
Line by line Explanation
import { NextSeo } from 'next-seo': This line imports theNextSeocomponent from thenext-seolibrary, which is used to set SEO information for a page.import { FunctionComponent } from 'react': This line imports theFunctionComponenttype from the React library, which is used to define the wrapped component as a functional component.import { ArticleProps, WithSeoProps } from '../components/Utils/Intf': This line imports two interfaces,ArticlePropsandWithSeoProps, from theIntffile in theUtilsfolder of thecomponentsfolder. These interfaces are used to describe the properties of the wrapped component and theseoPropsthat are passed to the HOC.const withSeo = (WrappedComponent: FunctionComponent<WithSeoProps>, seoProps: ArticleProps) => {: This line defines a functionwithSeowhich takes two arguments,WrappedComponentandseoProps.WrappedComponentis defined as a functional component with theWithSeoPropsinterface.seoPropsis defined with theArticlePropsinterface.const WithSeo: FunctionComponent<WithSeoProps> = (props) => (: This line defines a new constantWithSeowhich is also a functional component that takespropsas its parameter.<NextSeo {...seoProps} />: This line uses theNextSeocomponent and spread operator to pass theseoPropsto theNextSeocomponent.<WrappedComponent {...props} />: This line renders theWrappedComponentand passes thepropsto it as well.return WithSeo: This line returns theWithSeocomponentexport default withSeo: This line exports thewithSeofunction, which can be imported and used in other files.






