React Native:TextInput toUpperCase在onChangeText上的Android上不起作用

我有一个TextInput组件,应在键入时将输入转换为大写。我的代码如下:

import React,{Component} from 'react';
import { View,StyleSheet,Text,TextInput,Button } from 'react-native';

export default class ProfileTest extends React.Component {

 constructor(props) {
    super(props);
    this.state = {text : ''};
  }

  render() {
    return ( 
     <View>
          <TextInput
            style={{fontSize : 60}}
            onChangeText={text => {
              text = text
                .toUpperCase();
              this.setState({ text: text });
            }}
            value={this.state.text}
            placeholder="enter text"
          />

        </View>
    )
  }
}

在Expo上,这确实有效。但是,当我在Android设备上尝试此操作时,会出现以下现象:

React Native:TextInput toUpperCase在onChangeText上的Android上不起作用

前两个字母可以正常工作,但是每当我添加第三个字母时,它突然重复前两个字母,以便 ABC-> ABABC 我不知道为什么要这么做,而且我似乎无法摆脱它。我已将“ .toUpperCase()”确定为罪魁祸首。

感谢您的帮助!

tacky 回答:React Native:TextInput toUpperCase在onChangeText上的Android上不起作用

您可以在此处找到更多信息: https://github.com/facebook/react-native/issues/23578

1 人修复了它,但它仍未投入生产: https://github.com/facebook/react-native/pull/29070

我试图找到一些变通方法,比如改变状态、引用但没有任何作用:/

,
import React,{ useState } from 'react';
import { TextInput,Platform } from 'react-native';

const UselessTextInput = () => {
  const [info,setInfo] = useState('');

  return (
    <TextInput
      style={{ height: 40,borderColor: 'gray',borderWidth: 1 }}
      onChangeText={text => {
       setInfo(text.toLowerCase()) 
      }}
      secureTextEntry={Platform.OS === 'ios' ? false : true}
      keyboardType={Platform.OS === 'ios' ? null : 'visible-password'}
      value={info}
    />
  );
}

export default UselessTextInput;
本文链接:https://www.f2er.com/2904014.html

大家都在问