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

发布于 2025-02-10 12:48:57 字数 490 浏览 1 评论 0原文

我必须写一个有条件的陈述,以使一定百分比的价值绿色,如果其正红色为阴性,则为绿色。但是我不确定在哪里编写此条件语句。

<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>

我需要制作row.data.marketd显示颜色。

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

但是我不知道要在哪个标签中包含此条件。任何帮助都将受到赞赏。

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.

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

But I have no idea which tag to include this condition in. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰葑 2025-02-17 12:48:57

将答案视为一个概念。您的UI库可能会提供另一种方式。

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

Consider the answer as a concept. Your ui library may provide another way.

<span style={{color:row.data.MarketD<0 ? 'red' : 'green'}}>{row.data.marketD}</span>
删除→记忆 2025-02-17 12:48:57

所有条件陈述都应在实践的正方形内。

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

All of the condition statement should be inside the square of practice.

{row.data.marketD < 0 ? 'red':'green' }
西瑶 2025-02-17 12:48:57

另一种方法是将文本包裹在版式组件中,并根据条件更改颜色属性。通过这种方式,您将文本与grid的其余内容内容隔离开来(它不会影响今天旁边的文本):

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

Another approach is to wrap your text in a Typography component and change the color attribute based on your condition. 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):

<Typography color={row.data.MarketD<0 ? 'red' : 'green'}>
    Your text here
</Typography>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文