我目前正在学习 React Native,我正在尝试创建一个可以清除多个文本输入的清除按钮。我正在使用以下链接尝试制作清除按钮: https://snack.expo.io/B1xM4CjIb
我合并了这样的例子:
export default class Input extends Component {
handleLocation = (text) => {
this.setState({ location: text })
}
handleStartTime = (text) => {
this.setState({ startTime: text })
}
handleEndTime = (text) => {
this.setState({ endTime: text})
}
login = (location, startTime, endTime) => {
alert('Location: ' + location + 'Start Time:' + startTime + 'End Time:' + endTime )
}
clearInput = () => {
this.textInputRef.clear();
}
render (){
return (
<View style={styles.container}>
<TouchableOpacity
style = {styles.submitButton}
onPress = { () => this.login(this.state.location, this.state.startTime, this.state.endTime)}>
<Text style = {styles.submitButtonText}>
Submit
</Text>
</TouchableOpacity>
<Button title= "Clear" onPress={this.clearInput.bind(this)}/>
<TextInput
ref={ref => this.textInputRef = ref}/>
value={this.state.location, this.state.startTime, this.state.endTime}/>
</View>
当我运行它时,我收到错误 RawText 'Value=' must be wrapped in an explicit <Text> Component
.那只会使单词值出现在屏幕上,而清除按钮仍然不起作用。我该如何解决?感谢任何可以提供帮助的人。
最佳答案
你可以说
clearInput = () => {
this.setState({ location: '', startTime: '', endTime: '' });
}
同样,因为这个函数是一个箭头函数。在<Button>
我们可以说 onPress={this.clearInput}
不需要 bind(this)
.
关于javascript - 你如何在 React Native 中创建一个清晰的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45405477/