0

this is my code :

{
        delay > 0
        ? <strong className="history__late"> {delayDayConst}j {delayHourConst}h {delayMin}m <FormattedMessage {...globalMessages.inProgressAndLate} />
         </strong>
        : delay < 0 
        ? <span className="history__advance"> - {delayDayConst}j {delayHourConst}h {delayMin}m <FormattedMessage {...globalMessages.inProgressAndAhead} /></span>
        : <span className="history__break"><FormattedMessage {...globalMessages.break} /></span> 
 }

I only want to insert a CSS line on my condition, this CSS line point to another element not present in the function. I try some of things but no found a solution.

PS: sorry for my bad englsih.

4
  • The problem is not clear Commented Jul 12, 2018 at 12:28
  • @Harikrishnan When delay > 0, I want to change the color of another element in my page, so point the CSS of that element. Commented Jul 12, 2018 at 12:29
  • can you provide the code for that element too Commented Jul 12, 2018 at 12:32
  • @Harikrishnan It's just a <div> Commented Jul 12, 2018 at 12:35

1 Answer 1

1

You can use state for css class.Refer the following example.Here i consider the delay is passed as props

class App extends React.Component {
  constructor({ props }) {
    super(props);
    this.state = {
        class:'class1'
    };
  }
 componentWillMount() {
       if(this.props.delay >0 )
         { 
            this.setState({class:class1})
         }
       else {
                this.setState({class:class2})
            }
   }
  render() {
    return  <div><p className={this.state.class}>hello</p></div>; // replace with your condition
  }
}
Sign up to request clarification or add additional context in comments.

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.