我正在学习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/

10-16 08:37