N
N
nikmil2019-08-29 17:33:50
React
nikmil, 2019-08-29 17:33:50

How to change the text in react depending on the value?

Hello, I want to make it so that if the value is 0, then the color becomes red.. Here's how I did it, but it doesn't work)

<p style={{color: "{ (this.state.petrol > 0) ? '#ffffff' : '#D12828'}" }}>{this.state.petrol}l</p>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petr Muhurov, 2019-08-29
@nikmil

const color = this.state.petrol > 0 ? "#FFF" : "#D12828";

return (
  <p style={{ color }}>{this.state.petrol}</p>
);

I
Ivan V, 2019-08-29
@verkhoturov

<p style={{color: this.state.petrol > 0 ? '#ffffff' : '#D12828' }}>{this.state.petrol}l</p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question