javascript - 你如何在 React Native 中创建一个清晰的按钮

标签 javascript react-native

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

相关文章:

javascript - Ajax : check email availability with PHP

javascript - Angular 5 : HTTP post `Response for preflight has invalid HTTP status code 400.`

javascript - JavaScript 类之间的 React Native 变量

javascript - 清除警报消息

javascript - 从父目录 react 导入

ios - xcodebuild 失败,返回码为 : 65 - using CLI/Appcenter but no error messages

javascript - 仅获取大括号内的字符串

javascript - 使用 jQuery.attr() 转义属性值

javascript - 如何访问 Handlebars 中具有动态索引的对象中的数组?

javascript - React-Native:处理 babel-preset-react-native/index.js 的 TransformError