简体   繁体   English

我在哪里可以在反应类中编写条件语句?

[英]Where can I write a conditional statement in a react class?

I have to write a conditional statement to make a certain percentage value green if its positive and red if its negative.我必须编写一个条件语句来使某个百分比值如果为正则为绿色,如果为负则为红色。 But I am not sure where to write this conditional statement.但是我不确定在哪里写这个条件语句。

<div>
{this.state.tableData.map((row, index)=>{
return(

<Box component="span" sx={{}}>
<Grid > {row.data.direction}</Grid>
<Grid > ({row.data.marketD}%) today </Grid>
</Box>

)
})}
</div>

I need to make row.data.marketD show colour.我需要让row.data.marketD显示颜色。

color: {row.data.marketD}<0 ? 'red':'green'.

But I have no idea which tag to include this condition in. Any help is appreciated.但我不知道在哪个标签中包含这个条件。任何帮助表示赞赏。

所有的条件语句都应该在练习的方格内。

{row.data.marketD < 0 ? 'red':'green' }

Consider the answer as a concept.将答案视为一个概念。 Your ui library may provide another way.您的 ui 库可能会提供另一种方式。

<span style={{color:row.data.MarketD<0 ? 'red' : 'green'}}>{row.data.marketD}</span>

Another approach is to wrap your text in a Typography component and change the color attribute based on your condition.另一种方法是将文本包装在Typography组件中,并根据您的条件更改颜色属性。 In this way you isolate you text from the rest of the content content of the Grid (it will not affect the today text beside it):通过这种方式,您可以将文本与Grid的其余内容内容隔离开来(它不会影响它旁边的today文本):

<Typography color={row.data.MarketD<0 ? 'red' : 'green'}>
    Your text here
</Typography>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM