Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
How to split a React Component by words to apply a different styling to each word?

I have a React component that renders a page with a title and some content.

The Page Component renders the title by applying different styles to all the words (separated by a space) in the title.

It works fine when the title is a string (I can use title.split(' ')...), but I need to use a JSX element as the title in some occasion. I'm using React-Intl for the translation, and I'm using a <FormattedMessage id='...'/> to translate the title.

How can I split the FormattedMessage JSX element into multiple React components so that I can apply an individual style?

Here's a sandbox illustrating the problem:

Sandbox

Press the "Switch to page..." button to view what it should look like.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Splitting the FormattedMessage component during render isn't possible I think. Also, I had no luck to render it (during render...) to extract the string from the output.

What you could try to do to get a string and then render it like the other titles is to take the id from children.props.id and resolving it to a string (using the values you pass to IntlProvider if those are available at this point.

Edit:

Looking at it again, the proper way to do is to apply the formatting inside FormattedMessage like so:

// App.js line 7
<FormattedMessage id="title.main">
  {txt => <FormattedTitle>{txt}</FormattedTitle>}
</FormattedMessage>

This way children will already be formatted and you can use the existing component.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda