Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!