1

hi guys i want to change textcolor of input dynamically. for example initially in CSS placed as color:'#fff',then i want to change the text color whenever need as '#ddd'.

3
  • What have you tried doing? Could you paste any code to help us see your project markup. I would personally store the color as a state variable and simply do <TextInput style={{color: this.state.color}}/> Commented Jul 28, 2017 at 10:18
  • This dynamic condition will be based on the state's value. Right ? Commented Jul 28, 2017 at 10:26
  • yes, <TextInput style={{color: this.state.color}}/> its not working Commented Jul 28, 2017 at 10:30

3 Answers 3

2

You can use ternary operator to check your condition. It has following format: (test conditon) ? true statement: false statement

Based on that, you can set the <TextInput> color with something like this:

<TextInput
  style={[styles.input, (this.state.username ? styles.inputHighlight : null)]} //replace with your condition
/>

And add style stylesheet:

var styles = StyleSheet.create({
  input: {
    //other styles
    color:'#fff'
  },
  inputHighlight: {
    color:'#ddd'
  }
});  
Sign up to request clarification or add additional context in comments.

9 Comments

style={[(this.state.checkSwitch ? styles.inputColor : styles.v_stInput)]} this is my condition checking switch value based on that setting color but this is not working
<TextInput style={[(this.state.checkSwitch ? styles.inputColor : styles.v_stInput)]}/>
Are you sure that this.state.checkSwitch has your expected value? To check, add { console.log(this.state.checkSwitch) } somewhere in your render()
thnks have found where i made mistake
Glad! :) Mark this as correct ans/upvote if it was helpful :)
|
0

I have created sample app from expo to demonstrate dynamic color changes. I have used interval, but you can use as per your need with setColor function.

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
    constructor() {
        super();
        this.state = { colors: '#ffffff' };
    }
    setColor = (value) => {
        this.setState({ colors: value })
    }
    componentDidMount() {
        let i = 0;
        let colorsArray = ['gray', 'red', 'green', 'blue', 'pink', 'yellow']
        this._interval = setInterval(() => {
            if (i <= 5) {
                this.setColor(colorsArray[i]);
                i++;
            }
        }, 5000);
    }
    render() {
        return (
            <View style={styles.container}>
                <Text style={{ color: this.state.colors }}>
                    Change code in the editor and watch it change on your phone!
          Save to get a shareable url. You get a new url each time you save.
        </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
    },
});

4 Comments

The problem is TextInput, not Text. Can't do like that for TextInput in android
@TuanNguyen not sure if you had read the question or not, hi guys i want to change textcolor of input dynamically. and the above was working demo when I had posted my answer.
Which OS you built?
this demo was built and test in Android, but I had used similar kind of functionalities in both Android and iOS
-1

Similar to Khushboo Shah's answer, this is how I adjusted the color for my Component. The code must be placed in the render() function, and render() must be triggered using normal methods.

render() {

    var overrideStyle = {};
    if ( condition ) {
         overrideStyle = { color: '#ddd' };
    }
    return (
              <Text style={[ this.props.style, overrideStyle ]}>
                //...
              </Text>
    );

}

1 Comment

Can't do that for TextInput in Android, but works in iOS man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.