如何在按下回车键时使多行属性为假?



我希望搜索栏在按回车键时变空,而不是获得多行,即使多行属性为假。但我也观察到,要读取输入键本机事件多行属性应该是真的。

handlePress=(e)=>{
if(e.nativeEvent.key == "Enter"){
this.setState({searchTerm:''});
}
}
render(){
return(
<View style={styles.container}>
<FontAwesome name={'search'} style={styles.searchIcon}/>
<TextInput
placeholder='Search...'
value = {this.state.searchTerm}
multiline={true}
style={styles.input}
onKeyPress={this.handlePress}
/>
</View>
);
}

我能想到的解决方案是

state={
multiLine: false
}
handlePress=(e)=>{
if(e.nativeEvent.key == "Enter"){
this.setState({searchTerm:''});
}
this.setState({ multiLine:!this.state.multiLine })
}
render(){
return(
<View style={styles.container}>
<FontAwesome name={'search'} style={styles.searchIcon}/>
<TextInput
placeholder='Search...'
value = {this.state.searchTerm}
multiline={ this.state.multiLine }
style={styles.input}
onKeyPress={this.handlePress}
/>
</View>
);
}

所以我试过了,没有遇到任何问题,可能有其他原因导致这个问题,这是我所做的

<Input 
placeholder = "Task"
multiline={ this.state.multiLine }
style= {{ marginTop:8 }}
onChange= {e => this.setState({ task: e.nativeEvent.text })}
value= {this.state.task}
/>

最新更新