样式化组件:根据条件更改文本颜色

发布于 2025-01-10 22:50:33 字数 440 浏览 0 评论 0原文

我有一个表,如果交易金额 <= 0,则显示字符串提款,否则存款。

在这种情况下如何改变文本颜色?

我知道我需要使用道具,但似乎无法解决?

jsx:

     <TransactionType>
     {transaction.amount <= 0 ? "withdraw" : "deposit"}
     </TransactionType>

样式组件:

export const TransactionType = styled.div<ITransactionType>`
color: ${(prop) => (prop.TransactionType ? "red" : "green")};

`;

谢谢!

I have a table that renders out the string withdraw if the transaction amount is <= 0 else deposit.

How do I get the text color to change on this condition?

I know I need to use props, but can't seem to work it out?

jsx:

     <TransactionType>
     {transaction.amount <= 0 ? "withdraw" : "deposit"}
     </TransactionType>

Styled component:

export const TransactionType = styled.div<ITransactionType>`
color: ${(prop) => (prop.TransactionType ? "red" : "green")};

`;

Thanks!

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

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

发布评论

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

评论(1

深府石板幽径 2025-01-17 22:50:33
<TransactionType type={transaction.amount <= 0 ? 'withdraw' : 'deposit'} >
  {transaction.amount <= 0 ? "withdraw" : "deposit"}
</TransactionType>
export const TransactionType = styled.div<ITransactionType>`
color: ${({ type }) => (type === 'withdraw' ? "red" : "green")};

我将您的道具重命名为 type 以提高可读性,但概念是相同的。并使用 props 参数的解构来获取可直接访问的类型。

<TransactionType type={transaction.amount <= 0 ? 'withdraw' : 'deposit'} >
  {transaction.amount <= 0 ? "withdraw" : "deposit"}
</TransactionType>
export const TransactionType = styled.div<ITransactionType>`
color: ${({ type }) => (type === 'withdraw' ? "red" : "green")};

I renamed your prop to type for improved readability, but the concept is the same. And used destructing of the props argument to get type directly accessible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文