react-native - React Native 获取未定义不是对象错误

标签 react-native

我是 React Native 的新手。

当我在 Android 模拟器上运行代码时,出现此错误:

undefined is not an object, evaluating ChangeTextHandler.bind(this).

请帮忙解决这个问题。谢谢。

这是我的代码。我错过了什么吗?

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, TextInput, View} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

// type Props = {};
export default class App extends Component {
  constructor(){
    super()
    this.state= {
      value: "default text"
    }
    this.onChangeTextHandler = this.ChangeTextHandler.bind(this)
  }


  onChangeTextHandler(newText){
      this.setState(
        {
          value: newText
        }
      )
      }


  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text>Username:  </Text>
        <TextInput defaultValue = "username"
          onChangeText={this.onChangeTextHandler} />
        <Text> You are writing {this.state.value}</Text>
        <Text>Password:  </Text>
        <TextInput  />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

顺便说一句,我收到错误“看起来您的帖子大部分都是代码,请添加更多详细信息”。但我没什么可说的了。

最佳答案

我想你试试这个......

export default class App extends Component {
  constructor(){
    super()
    this.state= {
      value: "default text"
    }
  }


  onChangeTextHandler(newText){
      this.setState(
        {
          value: newText
        }
      )
      }


  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text>Username:  </Text>
        <TextInput defaultValue = "username"
          onChangeText={(text) => this.onChangeTextHandler(text)} />
        <Text> You are writing {this.state.value}</Text>
        <Text>Password:  </Text>
        <TextInput  />
      </View>
    );
  }
}

关于react-native - React Native 获取未定义不是对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51780056/

相关文章:

android - 我可以在 React Native 中要求专门针对 iOS 的模块吗?

React-Native 版本不匹配

android - Android Studio 中的 React Native 应用程序 : Cause Argument type mismatch

arrays - 如何正确地将数组映射到 <Text> (React native)

reactjs - React 中的 Context 始终是空对象

PHP加密数据需要在React Native中解密

firebase - Google 在 React Native 中使用 Firebase 登录

javascript - React Native - 在真实设备上构建时出现 XCode 错误?

reactjs - 您如何使用带样式的组件在 React Native 中为 Text 子项设置样式?

performance - 将react-addons-perf与react native结合使用