UWGOOSE
UWGOOSE

Reputation: 913

Adding favicon to <Helmet/> in React

I was trying to add a favicon in the <Helmet/> tag of a React App

my current <Helmet/> tag looks like this.

<Helmet
        title="ABC"
        meta={[
            { name: "ABC", content: "ABC" }
        ]}
        links={
        rel='icon',
        type='image/png',
        sizes='16x16',
        href={require('favicon.ico')}
        }

/>

But I'm getting an error about an unexpected token, any inputs on how to add a favicon? Thanks for the help in advance.

Upvotes: 15

Views: 23208

Answers (4)

Md Masud Rana
Md Masud Rana

Reputation: 11

To solve this problem we can add data-react-helmet="true" on the favicon link tag like this.

Your index.html page:

<html lang="en">
  <head>
    <link id="favicon" rel="icon" href="/favicon.ico" type="image/x-icon" data-react-helmet="true" />
  <head>
</html>

Your component page:

<Helmet>
  <link id="favicon" rel="icon" href="/new-favicon.ico" type="image/x-icon"/>
</Helmet>

Upvotes: 0

kbooth1000
kbooth1000

Reputation: 71

Lucas's solution almost worked for me except I needed to import the favicon into the react component and use it as a js variable:

import Helmet from 'react-helmet'
import favicon from './favicon.ico';
...

<Helmet>
  <title>ABC</title>
  <meta name="ABC" content: "ABC" />
  <link rel="icon" type="image/png" href={favicon} sizes="16x16" />
</Helmet>```

Upvotes: 2

sekercan cepni
sekercan cepni

Reputation: 71

Try this please. You written wrong keyword(links => link)

<Helmet
        title="ABC"
        meta={[
            { name: "ABC", content: "ABC" }
        ]}
        link={[
              {"rel": "icon", 
               "type": "image/png", 
               "href": "favicon.ico"
              }
             ]}
/>

Upvotes: 7

Lucas Kellner
Lucas Kellner

Reputation: 441

If you are using react-helmet in your project then I think one issue with your code is that link should be singular, not the plural 'links'

Try this

import Helmet from 'react-helmet'

...

<Helmet>
  <title>ABC</title>
  <meta name="ABC" content: "ABC" />
  <link rel="icon" type="image/png" href="favicon.ico" sizes="16x16" />
</Helmet>

Upvotes: 12

Related Questions