【问题标题】:Text Color not changing in React NativeReact Native 中的文本颜色没有变化
【发布时间】:2019-11-14 14:20:33
【问题描述】:

所以我正在使用 Typescript 搞乱 React Native,我试图实现一个暗模式系统(一旦我让它工作,它将进入 Redux 状态)但是,我的背景正在改变颜色,但我的文本没有。

我看到React Native Text color not working 我的状态正在更新,因为我的背景颜色正在改变。

import React from 'react';
import { StyleSheet, Text, StatusBar, SafeAreaView, AsyncStorage, Button } from 'react-native';

export interface Props{
  name: string;
}

export interface State{
  darkmode: boolean;
  ready:boolean;
}

export interface DarkModeStyle{
  backgroundColor: string;
  color: string
}

export interface LightModeStyle{
  backgroundColor: string;
  color: string
}

export default class BeaconAppFrame extends React.Component<Props, State>{
  private styles;

  public state : State = {
    darkmode:false,
    ready:false
  }

  public darkModeStyle : DarkModeStyle = {
    backgroundColor:"#555", 
    color:"#EEE"
  }

  public lightModeStyle : LightModeStyle = {
    backgroundColor:"#FFF",
    color:"#000"
  }

  constructor(props: Props){
    super(props);

    // setup the default frame styles
    this.styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'flex-start',
        justifyContent: 'flex-start',
      }
    });
  }

  componentDidMount(){
    AsyncStorage.getItem('darkmode').then( (darkmode) => {
      this.setState({darkmode:darkmode === "yes", ready:true});
    }).catch(() => {
      this.setState({darkmode:false, ready:true});
      AsyncStorage.setItem("darkmode", "no");
    });
  }

  enableDarkMode(){
    this.setState({"darkmode":!this.state.darkmode});
    AsyncStorage.setItem("darkmode", this.state.darkmode?"yes":"no");
  }

  render(){
    if(this.state.ready){
      let style = [this.styles.container, this.state.darkmode?this.darkModeStyle:this.lightModeStyle];
      console.log(style);
      return (
        <SafeAreaView style={style}>
          <StatusBar barStyle="light-content" hidden={true} translucent={false} />
          <Text>darkmode is set to {this.state.darkmode? "Active":"Inactive"}</Text>
          <Button onPress={this.enableDarkMode.bind(this)} title={"Toggle darkmode"}>
            <Text>Toggle Darkmode</Text>
          </Button>
        </SafeAreaView>
      );
    }else{
      return (
        <SafeAreaView style={this.styles.container}>
            <Text>Please Wait Loading</Text>
        </SafeAreaView>
      )
    }
  }
}

【问题讨论】:

  • 您没有将任何样式传递给Text 组件,我可以看到

标签: typescript react-native


【解决方案1】:

你必须直接在 Text 组件上设置颜色样式属性。

【讨论】:

    【解决方案2】:

    这应该可行:

    <Text style={{ color: this.state.darkmode ? "#EEE" : "#000" }}>darkmode is set to {this.state.darkmode? "Active":"Inactive"}</Text>
    

    基本上,您需要将样式类中的color 应用到Text 组件,而不是应该只保留backgroundColorSafeAreaView

    【讨论】:

    • 是的,刚刚发现了这一点,打破了全局样式的观点,尽管它现在意味着对于应用程序中的每个文本,我必须对样式进行评估...
    • @MartinBarker 更好的解决方案是为文本创建一个新组件。所以你不必每次都传递样式。
    • 这就是我现在得到的,pastebin.com/ZTFVANzp,但是创建一个组件如果没有传递给它,它如何访问最顶层组件中的某些东西?
    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 2020-08-08
    • 2022-08-19
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多