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

176
Views
How can I dynamically change part of text inside a reactjs render?

I have a row of text that should change depending on two different conditions. The first condition works well, it changes the text back and forth between 'Completed: ' and and empty string ''. As below.

render() {
    return(
    <div className={container}>
                {(this.conditionOne ? 'Completed: ' : '') + this.completedValue + '%'}
    </div>
    )
}

The second condition is what I can't get working. It is only supposed to change the second part of the text, i.e. this.completedValue + '%'. My first instinct was to wrap that part up in a div and then change the CSS for that specific div as I tried here.

render() {
    return(
            <div className={container}>
                {(this.conditionOne ? 'Completed: ' : '') + 
                    <div className={this.conditionTwo? 'conditionTwoDiv' : ''}>
                        {this.completedValue + '%'}
                    </div>
                }
            </div>
    )
}

This only renders "Completed: {Object Object}". The only thing I want to do is to change the color of the text when this.ConditionTwo is true´, but I can't figure out how to do this without either having a huge nest of conditions or changing the whole text and not just the last part.

As you probably understand from question I'm not the best with this syntax or programming in general so explanations that help me understand why something is working or not are extra appreciated. Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use span to add string like this:

<span>
  {this.conditionOne && "Completed: "}
  <span className={this.conditionTwo ? "conditionTwoDiv" : ""}>{this.completedValue + "%"}</span>
</span>
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!