最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TextInput onChangeText not working React Native - Stack Overflow

programmeradmin0浏览0评论

i am very new to this platform , it my 2nd day on this. i trying to learn it by myself by creating simple login , sign up , news feed application . i am able to build login page but while getting values from InputText , it display me error saying "Can not specify both value and Children" , Here i am posting my login.js class

export default class LoginScreen extends Component {

    constructor(props) {
        super(props);
        this.state = {
            username: '',
            password: ''
        };
    }
    login = () => {
        alert(this.state.username + " " + this.state.password);
    }
    render() {
        return (
            <View style={styles.container}>

                <Image source={require('../images/hulk.jpg')} style={styles.backgroundimage}>
                    <View style={styles.content}>
                        <Image source={require('../images/logo.png')} style={styles.logo}>
                        </Image>

                        <View style={styles.inputcontainer}>

                            <TextInput underLineColorAndroid='transparent' style={styles.input} value={this.state.username} onChangeText={(username) => this.setState({ username })} placeholderText='username' > </TextInput>

                            <TextInput secureTextEntry={true} underLineColorAndroid='transparent' style={styles.input} value={this.state.password} onChangeText={(password) => this.setState({ password })} placeholderText='password' placeholderTextColor='black'> </TextInput>

                        </View>
                        <TouchableOpacity onPress={this.login} style={styles.buttonContainer}>
                            <Text style={styles.buttonText}>LOGIN</Text>
                        </TouchableOpacity>
                    </View>
                </Image>
            </View>
        );
    }
}

Please let me know where i am going wrong in code

Regards

i am very new to this platform , it my 2nd day on this. i trying to learn it by myself by creating simple login , sign up , news feed application . i am able to build login page but while getting values from InputText , it display me error saying "Can not specify both value and Children" , Here i am posting my login.js class

export default class LoginScreen extends Component {

    constructor(props) {
        super(props);
        this.state = {
            username: '',
            password: ''
        };
    }
    login = () => {
        alert(this.state.username + " " + this.state.password);
    }
    render() {
        return (
            <View style={styles.container}>

                <Image source={require('../images/hulk.jpg')} style={styles.backgroundimage}>
                    <View style={styles.content}>
                        <Image source={require('../images/logo.png')} style={styles.logo}>
                        </Image>

                        <View style={styles.inputcontainer}>

                            <TextInput underLineColorAndroid='transparent' style={styles.input} value={this.state.username} onChangeText={(username) => this.setState({ username })} placeholderText='username' > </TextInput>

                            <TextInput secureTextEntry={true} underLineColorAndroid='transparent' style={styles.input} value={this.state.password} onChangeText={(password) => this.setState({ password })} placeholderText='password' placeholderTextColor='black'> </TextInput>

                        </View>
                        <TouchableOpacity onPress={this.login} style={styles.buttonContainer}>
                            <Text style={styles.buttonText}>LOGIN</Text>
                        </TouchableOpacity>
                    </View>
                </Image>
            </View>
        );
    }
}

Please let me know where i am going wrong in code

Regards

Share edited Nov 26, 2018 at 9:20 seulgibear 6031 gold badge6 silver badges17 bronze badges asked May 19, 2017 at 7:44 user2028user2028 1834 gold badges17 silver badges43 bronze badges 2
  • Can you please update your code there apply proper formatting so i can read and help you. – Nisarg Thakkar Commented May 19, 2017 at 7:57
  • @NisargThakkar , please check – user2028 Commented May 19, 2017 at 8:03
Add a ment  | 

1 Answer 1

Reset to default 2

Can you please try this

export default class LoginScreen extends Component {

constructor(props) {
    super(props);
    this.state = {username: '',password: ''};
}
login = () => {
    alert(this.state.username + " " + this.state.password);
}
render() {
    return (
        <View style={styles.container}>
            <Image source={require('../images/hulk.jpg')} style={styles.backgroundimage}>
                <View style={styles.content}>
                    <Image source={require('../images/logo.png')} style={styles.logo}>
                    </Image>
                    <View style={styles.inputcontainer}>
                        <TextInput 
                        underLineColorAndroid='transparent' 
                        style={styles.input} 
                        value={this.state.username} 
                        onChangeText={(text) => this.setState({ username: text })} 
                        placeholderText='username'/>

                        <TextInput 
                        secureTextEntry={true} 
                        underLineColorAndroid='transparent' 
                        style={styles.input} 
                        value={this.state.password} 
                        onChangeText={(text) => this.setState({ password:text })} 
                        placeholderText='password' 
                        placeholderTextColor='black'/>
                    </View>
                    <TouchableOpacity onPress={this.login()} style={styles.buttonContainer}>
                        <Text style={styles.buttonText}>LOGIN</Text>
                    </TouchableOpacity>
                </View>
            </Image>
        </View>
    );
}
发布评论

评论列表(0)

  1. 暂无评论