react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Using provided example - Uncaught Error: Helmet expects a string as a child of <title>.

Open csi-lk opened this issue 7 years ago • 10 comments

Using the first example in the readme.md

import React from "react";
import {Helmet} from "react-helmet";

class Application extends React.Component {
    return (
        <div className="application">
            <Helmet>
                <meta charSet="utf-8" />
                <title>My Title</title>
                <link rel="canonical" href="http://mysite.com/example" />
            </Helmet>
            ...
        </div>
    );
};

I get:

Uncaught Error: Helmet expects a string as a child of <title>. Did you forget to wrap your children in braces? ( <title>{``}</title> ) Refer to our API for more information.
at HelmetWrapper.warnOnInvalidChildren

Even if I change it to

<title>{'My Title'}</title>

I get the same issue

Any ideas?


Edit see workaround below: https://github.com/nfl/react-helmet/issues/274#issuecomment-345241784

csi-lk avatar May 02 '17 22:05 csi-lk

Our unit tests definitely test this case. My Title should be a string. Hmmm.... What's your version of React?

cwelch5 avatar May 04 '17 18:05 cwelch5

I'm also getting this, though if I use <Helmet title="My Title"> instead it works. Running React 15.5.4.

Some elements work such as meta and link, but this doesn't for e.g. (from README):

<script src="http://include.com/pathtojs.js" type="text/javascript" />
Error: Helmet expects a string as a child of <script>. Did you forget to wrap your children in braces? ( <script>{``}</script> ) Refer to our API for more information.
    at HelmetWrapper.warnOnInvalidChildren

I'm using hyperx instead of JSX.

AlecRust avatar May 16 '17 15:05 AlecRust

I encountered similar problem with following code:

<Helmet>
    <title>Product - { item.name }</title>
</Helmet>

Fixed using string literal

<Helmet>
    <title>{ `Product - ${ item.name }` }</title>
</Helmet>

atulmy avatar Nov 17 '17 13:11 atulmy

yep just ran into this as well. Above solution is a good workaround.

joshkurz avatar Dec 15 '17 02:12 joshkurz

This is rather annoying when trying to do internationalization. Just allow a node.

bramski avatar Nov 25 '18 00:11 bramski

Would love it if nodes were allowed as well. It just seems a bit silly at the moment. It's not that much more, but the first is much easier to read.

Right now, I have to turn this:

<Helmet>
  <title>Default Title{subtitle && `: ${subtitle}`}</title>
</Helmet>

Into this:

<Helmet>
  <title>{subtitle ? `Default Title: ${subtitle}` : `Default Title`}</title>
</Helmet>

mikedpad avatar Dec 08 '18 20:12 mikedpad

Just like OP I'm getting these warnings even though all my titles are explicitly wrapped as strings. This is more annoying than anything but would like to see this fixed.

[email protected]

aforty avatar Sep 24 '20 15:09 aforty

@mikedpad or you can write it, if you prefer, like so:

<Helmet>
  <title>{`Default Title${ subtitle ? ': '+subtitle : '' }`}</title>
</Helmet>

atulmy avatar Sep 24 '20 16:09 atulmy

I encountered similar problem with following code:

<Helmet>
    <title>Product - { item.name }</title>
</Helmet>

Fixed using string literal

<Helmet>
    <title>{ `Product - ${ item.name }` }</title>
</Helmet>

Thank you! Your suggestion worked perfect!

codler avatar Oct 25 '20 07:10 codler

Thank you. @codler

sahilbakoru avatar Jun 01 '22 07:06 sahilbakoru