【问题标题】:Unable to type inside text-input in React Native无法在 React Native 中输入文本
【发布时间】:2020-07-01 15:37:52
【问题描述】:

我有以下课程:

export default class DonationFormScreen extends React.Component {

  constructor(props) {
    super(props);
    
    this.state = {
      title: this.props.title,
    };

  }
  
  state = {
    units: '0',
    comment: '-'
  }


  render() {
          let institution=this.props.route.params.institution;
          let title = this.props.route.params.title;
          return ( 
          <View>
            <View style={styles.container}>
              <Text style={styles.label}>¿Cuántas unidades?</Text>
              <TextInput
                style={styles.input}
                placeholder="Unidades..."
                onChangeText={(text) => this.setState({units:text})}
              />
              <Text style={styles.label}>¿Algún otro comentario?</Text>
              <TextInput
                style={styles.input}
                placeholder="Comentar..."
                onChangeText={(text) => this.setState({comment:text})}
              />
              <View style={styles.button}>
                <Button color="white" title="Donar"/>
              </View>
            </View>  
          </View>
      );
    }
}

原来我无法在我的文本输入中输入。每当我点击它们时,占位符就会消失,它似乎让我可以打字,但屏幕上什么也没有写。

【问题讨论】:

  • 您需要为 TextInputs 设置值。

标签: javascript react-native


【解决方案1】:

您没有为文本输入赋予初始值

<TextInput
                value={this.state.units}
                style={styles.input}
                placeholder="Unidades..."
                onChangeText={(text) => this.setState({units:text})}
              />

评论 TextInput 也是如此

<TextInput
                value={this.state.comment}
                style={styles.input}
                placeholder="Comentar..."
                onChangeText={(text) => this.setState({comment:text})}
              /> 

【讨论】:

  • 似乎没有任何改变
  • 尝试在构造函数中将状态更改为:this.state={title: this.props.title, comment: '', units:''
  • 谢谢,原来我还必须将输入样式的高度增加到 50 而不是 40。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2016-12-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多