无法看到我在TextInput中输入的内容

时间:2019-06-21 05:30:40

标签: react-native react-native-android

我看不到我在TextInput上键入的任何文本。正在输入文本,但我看不到。

我尝试使用state并设置TextInput的样式,但是仍然无法使用。

import React, {Component} from 'react';
import {View} from 'react-native';
import {Button} from './Common';

class LoginForm extends Component {

state = {text: ''}

render(){
    return(
        <View>
            <View>
                <TextInput 
                    value={this.state.text}
                    onChangeText={text => this.setState({text})}
                    style={{height: 20, width: 100}}
                    placeholder={"Can you see this?"}
                    placeholderTextColor={"red"} 
                />
            </View>

            <View>
                <Button>
                    Log In
                </Button>
            </View>
        </View>
        );
    }
}

1 个答案:

答案 0 :(得分:0)

指定的值不同。

class LoginForm extends Component {

state = {text: ''}
...
    <View style={ alignItems: "center",justifyContent: "center",flex: 1}>
<TextInput 
                    value={state.text}
                    onChangeText={text => this.setState({text})}
                    placeholder={"Can you see this?"}
                    placeholderTextColor={"red"}
                    style={{height: 20, width: 100}}
                />
 </View>

}

OR

class LoginForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: ''
    };
  }
...


     <View style={ alignItems: "center",justifyContent: "center",flex: 1}>

                <TextInput 
                    value={this.state.text}
                    onChangeText={text => this.setState({text})}
                    style={{height: 20, width: 100}}
                />


                <Button>
                    Log In
                </Button>

        </View>

}