Я бы использовал существующий компонент Head , вместе с контекстом и состоянием, а так же кастомный HeadProvider:
// _app.js
import {HeadProvider} from './HeadProvider';
function App({Component, pageProps}) {
return (
<HeadProvider>
<Component {...pageProps} />
</HeadProvider>
);
}
// HeadProvider.js
import React, {createContext, useContext, useState} from 'react';
const HeadContext = createContext();
export const useHead = () => useContext(HeadContext);
export function HeadProvider({children}) {
const [headData, setHeadData] = useState({
title: 'Init title',
description: 'Init description',
keywords: 'Init keywords'
});
return (
<HeadContext.Provider value={{headData, setHeadData}}>
{children}
</HeadContext.Provider>
);
}
// MyBestHead.js
import Head from 'next/head';
import {useHead} from './HeadProvider';
export function MyBestHead() {
const {headData} = useHead();
return (
<Head>
<title>{headData.title}</title>
<meta name="description" content={headData.description}/>
{/* more head items */}
</Head>
);
}
// MyComponent.js
import React, {useCallback} from 'react';
import {useHead} from './HeadProvider';
export function MyComponent() {
const {setHeadData} = useHead();
const handleClick = useCallback(() => {
setHeadData({
title: 'New title',
description: 'New description',
keywords: 'New keywords'
});
}, [setHeadData]);
return (
<button onClick={handleClick}>Update Head Data</button>
);
}