android - 在 android 上使用状态时 react native textInput 中的错误

标签 android react-native textinput

此错误仅存在于 Android 上。

如果我在 Android 上有一个 textInput 并将值设置为状态。然后在其他地方我更改值,当我在 textInput 上使用 onChange 时,它​​使用旧文本值而不是新状态。

这是我的零食 https://snack.expo.io/SyV1mkIc4

下面是显示它不起作用的完整代码

    import * as React from 'react';
import { Text, View, StyleSheet, TextInput, TouchableOpacity } from 'react-native';
import { Constants } from 'expo';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      text:  'aaa'
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <TextInput
          value={this.state.text}
          onChange={(event) => this.setState({
      text: event.text,
    })}
        />
      <TouchableOpacity
        onPress={()=>this.setState({
      text: "",
    })}
        style={styles.submit}
      >
      </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  submit: {
    height: 200,
    width: 200,
    backgroundColor: 'blue',
    textAlign: 'center',
  },
});

最佳答案

改用onChangeText

onChangeText={(text)=>{this.setState({text});}}

关于android - 在 android 上使用状态时 react native textInput 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745333/

相关文章:

android - 相机尺寸和4 :3 aspect ratio is not matching

reactjs - react 流 : Cannot resolve module react-native

Jquery .keypress 动态添加的输入

android - 野生滚动输入安卓

android - TvInputManager.getTvInputList() 中的 NullPointerException

android - adb 重启未完成

javascript - 如何在React组件中进行两次条件检查

javascript - 为什么 this.state 在 React Native 中未定义?

android - Droid 中未显示 HTML 文本输入

java - ImageView坐标与Bitmap像素的对应关系——Android