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

Allow components that return strings to be children of title

Open ntucker opened this issue 8 years ago • 6 comments

Now that react 16 allows strings to be returned from components it would be prudent to allow such things when using react-helmet.

ntucker avatar Nov 02 '17 21:11 ntucker

We ran into this exact problem today with react-intl. Ideally we'd like to:

<Helme>
   <title><FormattedMessage {...messages.title}>{identity}</FormattedMessage></title>
</Helmet>

We also get the dreaded Helmet expects a string as a child of <title>. error when we try this. Would be great if this could work.

Happy to be involved in creating a PR for this if it's desired by the creators. Thanks for helmet!

JamieDixon avatar Nov 13 '17 15:11 JamieDixon

I just ran into this as well. Not the best solution but this was our work around.

          <FormattedMessage id='reports.budget.blocks.title' defaultMessage='Budget Cap Blocks'>
            {(title) => (
              <Helmet>
                <title>{title}</title>
              </Helmet>
            )}
          </FormattedMessage>

nmaves avatar Feb 09 '18 23:02 nmaves

Any updates on this? Still happening in 2019 and would be really useful.

kinging123 avatar Feb 03 '19 10:02 kinging123

@kinging123 React hook will be out in a few days. It might be that Helmet isn’t needed?

JamieDixon avatar Feb 03 '19 16:02 JamieDixon

@JamieDixon helmet is more of a portal pattern than something that works best with hooks.

ntucker avatar Feb 03 '19 16:02 ntucker

Hello,

I think I found a solution :

import { FormattedMessage, useIntl } from 'react-intl';

function toto() {
  const intl = useIntl();

  return (
    <FormattedMessage 
      id={an.intl.id.not.found.so.default.message.id.used} 
      defaultMessage={intl.formatMessage({ id: 'an.intl.id' })} 
    />

    <Field
      type="text"
      placeholder={intl.formatMessage({ id: 'an.intl.id' })}
    />
  )
}

It worked for me.

EmmanuelPREVOST avatar Sep 22 '20 16:09 EmmanuelPREVOST