如何在编辑结束后在TextInput中添加文本React Native



我的react本机应用程序中有一个URL textInput。当用户在没有指定URL协议的情况下键入URL时(例如"www.google.com"(,我需要自动添加"http://"(http://www.google.com)在用户结束输入之后。这是我的代码:

onChangeText={url => {
this.setState({ classUrl: url });
}}
onEndEditing={() => {
if (this.state.classUrl.indexOf('http') === -1) {
this.setState(
{ classUrl: `http://${this.state.classUrl}` },
() => {
console.log(this.state.classUrl)
}
);
}
}}
value={this.state.classUrl}

我的组件状态发生了更改,它更新了URL,但是更改不会显示在UI上。

您可以添加onEndEditing->链路

<TextInput
onChangeText={url => {
this.setState({ classUrl: url });
}}
onEndEditing ={()=> {
this.setState({ classUrl: 'https'+this.state.classUrl });
}}
/>

最新更新