【问题标题】:How to toggle placeholdertextcolor value in textinput?如何在文本输入中切换占位符文本颜色值?
【发布时间】:2019-08-26 00:14:30
【问题描述】:

我想做一个深色模式和浅色模式,在深色模式下我想要占位符textcolor white,在浅色模式下我想要深色。为了实现这一点,我在 placeholdertextcolor 本身中尝试了 if/else,我还尝试使用如下代码所示的函数来实现。

我使用来自 react-native 的 Switch,它返回 true 或 false。真时执行语句中的第一种样式,假时执行语句中的第二种样式。这些 placeholderTextColor 在 texinput 中。

// First try
const toggle = this.state.switchValue;
<TextInput
 style={textInput}
 onChangeText={(password) => this.setState({password})}
 placeholder={'Password'}
 placeholderTextColor={toggle === true ? styles.darkColor : styles.whiteColor}
 value={this.state.password}
/>

// Second try by doing it through a function
test = () => {
    return toggle === true ? styles.darkColor : styles.whiteColor
};
<TextIn...
placeholderTextColor={() => this.test()}
/>

// Third try. From the statement above I get an object, so I get the color (string ("#444")). But still get same error.
placeholderTextColor={toggle === true ? JSON.stringify(styles.darkColor.color) : JSON.stringify(styles.whiteColor.color)}

我希望占位符根据开关的状态改变颜色。但我得到的只是错误;警告:失败的道具类型:警告:失败的道具类型:无效的道具无效的道具placeholderTextColor提供给TextInput:函数。有什么办法可以做到这一点?

【问题讨论】:

  • 我认为我们需要了解更多相关信息。

标签: react-native


【解决方案1】:

根据 documentation 的 TextInput "placeholderTextColor" 应该接受 "color" 道具并且您正在使用 style 。

试试这个

const toggle = this.state.switchValue;
<TextInput
 style={textInput}
 onChangeText={(password) => this.setState({password})}
 placeholder={'Password'}
 placeholderTextColor={toggle === true ? "black" : "white"}
 value={this.state.password}
/>

【讨论】:

  • 哇,你是对的。我不明白为什么我的代码不起作用,因为它还返回一个带有颜色代码的字符串,如果它起作用它就起作用。谢谢大佬!
猜你喜欢
  • 2016-12-19
  • 1970-01-01
  • 2014-08-23
  • 2015-10-20
  • 1970-01-01
  • 2014-03-22
  • 2021-04-11
  • 1970-01-01
  • 2022-07-02
相关资源
最近更新 更多