0

I have a React component named "Badge" returning some Text that is styled bold.

render() {
 return (
  <div>
   <Text weight={'bold'}>
    {this.props.label}
   </Text>
  </div>
)

Now, I use "Badge" in another component named "Card".  

Because in Badge component all Text is styled bold, the whole sentence is bold as well:

return (
 <div>
  <Badge lable={'This part of the sentence is ' + 'normal font style' + ' and the rest is bold.'}></Badge>
 </div>
)

Now, I would like to style the items in this element differently: I want to remove the bold style from 'normal font style' - only for this single part of the string. I already tried this way, but it doesn't work:

<Badge label={'This part of the sentence is ' + <span class=”normalText”>'normal font style'</span> + ' and the rest is bold.'}></Badge>

As an absolute beginner, this problem is driving me crazy. Any ideas how to solve this?

6
  • try using className instead of class. Commented Sep 9, 2019 at 7:33
  • Expose the Text weight prop to your Badge API, i.e. <Badge label='This is bolded text!' weight='bold' /> Commented Sep 9, 2019 at 7:35
  • Does the Badge do anything other than contain s div and a Text component? Perhaps you could rewrite it a bit to be more of a container and it simply renders whatever children it contains (with whatever style they may have). Commented Sep 9, 2019 at 7:38
  • Hello Drew, the problem is, I am not allowed to modify the badge component. I have to use it as it is :( Commented Sep 9, 2019 at 7:40
  • Well then you may have to create a new component then as React will pretty much escape everything it renders (save for some judicious use of dangerouslySetInnerHTML which wouldn't work here as the Badge is only rendering a label into another react component). Why are you barred from modifying Badge? Commented Sep 9, 2019 at 8:05

2 Answers 2

1

If you want to pass the text with html tag then you have to pass all the text/tags in html form. Check below code.

import React, { Component } from 'react';
import ReactDOM,{ render } from 'react-dom';
import Badge from './Badge';
import './style.css';

class App extends React.Component {
  render() {
    return (
      <div>       
       <Badge label="test" /><br/>
       <Badge label={'This part of the sentence is ' + 'normal font style' + ' and the rest is bold.'} /><br/>
       <Badge label={<>
          <span>This part of the sentence is</span> <span className="normalText">'normal font style'</span> <span> and the rest is bold.</span> 
        </>}
       />
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

I have created small demo for you.
https://stackblitz.com/edit/react-rvuqv6

Hope this will work for you!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! Unfortunately, this version throws the error: "Type 'Element' is not assignable to type 'string'."
I am using custom component where pass details in label as props. If you are still facing same issue then please let me know the name of package for Text or Badge.
0

The more appropriate system is take label value as badge's children. to declare Badge component code like this example:

const Badge = props => {
    return (
       <div className={`badge-class`}>
          <Text weight={'bold'}>
              {props.children}
          </Text>
       </div>
    )
}

and now call the component like this

return (
   <div>
      <Badge>
          This part of the sentence is <span className={`normal-texts`}>normal font style</span> and the rest is bold.
      </Badge>
   </div>
)

with this you can pass any type children on Badge component, text, image etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.