Search code examples
reactjs

How to update meta tags in React.js?


I was working on a single page application in react.js, so what is the best way to update meta tags on page transitions or browser back/forward?


Solution

  • I've used react-document-meta in an older project.

    Just define your meta values

    const meta = {
        title: 'Some Meta Title',
        description: 'I am a description, and I can create multiple tags',
        canonical: 'http://example.com/path/to/page',
        meta: {
            charset: 'utf-8',
            name: {
                keywords: 'react,meta,document,html,tags'
            }
        }
    

    and place a

    <DocumentMeta {...meta} />
    

    in the return