可以根据值更改文本颜色吗

可以根据值更改文本颜色吗

本文介绍了可以根据值更改文本颜色吗?(反应原生)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 React Native 开发一个应用程序,这里是我遇到麻烦的代码:

I am working in an app using React native, here goes the code that I am in trouble:

import React, { Component } from "react";
import { Text, StyleSheet } from "react-native";
import { Content, Card, CardItem, Body, Left } from "native-base";
import { PricingCard } from "react-native-elements";

export default class AppBodyData extends Component {
  render() {
    let articles = this.props.data.map(function (articleData, index) {
      return (
        <PricingCard
          //color='#ff3300'
          wrapperStyle={
            articleData.percent_change_1h >= 0 ? styles.green : styles.red
          }
          info={[
            "1h change: " + articleData.percent_change_1h,
            "24h change: " + articleData.percent_change_24h,
            "7days change: " + articleData.percent_change_7d,
          ]}
          button={{
            title: "More information",
            icon: "info",
            backgroundColor: "#4f9deb",
          }}
        />
      );
    });

    return <Content>{articles}</Content>;
  }
}

const styles = StyleSheet.create({
  green: {
    color: "#00ff00",
  },
  red: {
    color: "#ff0000",
  },
});

module.export = AppBodyData;

我需要的是,如果 articleData.percent_change_1h 变量为正值该变量的颜色必须是绿色,否则必须是红色.

What I need is that if the articleData.percent_change_1h variable is positive the color of that variable must be green otherwise must be red.

PricingCard 是 react-native-elements 库的一个对象:https://react-native-training.github.io/react-native-elements/API/pricing/

PricingCard is an object of react-native-elements library: https://react-native-training.github.io/react-native-elements/API/pricing/

提前致谢

推荐答案

您可以使用 三元运算符 来操作传递给您的 PricingCard's wrapperStylestyles> prop.

You could use a ternary operator to manipulate styles passed to your PricingCard's wrapperStyle prop.

// Stylesheet.
import { StyleSheet } from 'react-native'

// Card.
<PricingCard wrapperStyle={(articleData.percent_change_1h >= 0) ? styles.green : styles.red} ../>

// Styles.
const styles = Stylesheet.create({
  green: {
    color: '#00ff00'
  },
  red: {
    color: '#ff0000'
  }
})

这篇关于可以根据值更改文本颜色吗?(反应原生)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:40