简体   繁体   English

undefined 不是 React Native 中的对象(评估函数)

[英]undefined is not an object (evaluating function) in React Native

Hi I'm new in react native and I'm working on a sample chat app.Currently I'm facing a basic problem of navigation.嗨,我是 React Native 的新手,我正在开发一个示例聊天应用程序。目前我正面临一个基本的导航问题。 Any help is appreciated.任何帮助表示赞赏。

I need to navigate from chat list to chat screen on click.我需要在点击时从聊天列表导航到聊天屏幕。 Im using android emulator using Android studio.我使用 Android Studio 使用 android 模拟器。 For this I wrote the script.为此,我编写了脚本。 But Im getting error when I click on a right_arrow having onPress function defined in the following code,但是当我单击具有以下代码中定义的 onPress 函数的 right_arrow 时出现错误,

onContactPress = (item) =>{
      Alert.alert();        
}    

renderFlatListItem({item}) {
  return (
    <View style={styles.container}>
      <TouchableOpacity onPress={() => this.onContactPress(item)} >
        <Image style={styles.rightArrow}
          source={require('../../images/right_arrow.png')}/> 
      </TouchableOpacity>
    </View>
   )  
}

and the the error is错误是

undefined is not an object (evaluating '_this3.onContactPress()')

What I'm doing wrong here?我在这里做错了什么? Is there a way to pass the contact details of the selected person to the next screen?有没有办法将所选人员的联系方式传递到下一个屏幕? Please help me out.请帮帮我。

This is in a React Class o functional component ?这是在 React Class o 功能组件中?

In case of the 2nd call simply onContactPress without this, if your case is the first maybe you have a babel issue.如果第二次调用只是 onContactPress 而没有这个,如果您的情况是第一个,那么您可能遇到了 babel 问题。

ok try this solution好的试试这个解决方案

change your onContactPress更改您的 onContactPress

from来自

onContactPress = (item) =>{
  Alert.alert();        
 }    

to

onContactPress({item}) {
      Alert.alert();        
}    

because fat arrow functions should be defined outside the class因为胖箭头函数应该在类之外定义

You probably have solved the issue by now.你现在可能已经解决了这个问题。 I'm new to React-Native myself and I came across your question after facing the same issue and I realized that the answer was in front of me all along.我自己是 React-Native 的新手,在遇到同样的问题后我遇到了你的问题,我意识到答案一直在我面前。

Error Prone Script容易出错的脚本

I have a component with a state that contains an array of names created using the constructor method.我有一个组件,其状态包含使用构造函数方法创建的名称数组。 Ideally, when rendered this is the layout I was expecting on my Android device A list of User names and from there since I have assigned a key attribute and the value of the name's array index, when a user name is clicked I wanted to console.log(i), (i) being the index provided as a argument to the getValue() function in the onPress={} event listener.理想情况下,渲染时这是我在我的 Android 设备上期望的布局用户名列表,因为我已经分配了一个键属性和名称数组索引的值,当单击用户名时我想控制台。 log(i), (i) 是作为参数提供给 onPress={} 事件侦听器中的 getValue() 函数的索引。

But instead this is what I was getting the TypeError: undefined is not an object (evaluating 'this.getValues') error.但相反,这就是我得到的TypeError: undefined is not an object (evaluating 'this.getValues')错误。

I tried reading docs, I googled, I looked at previously working codes, I did everything and nothing worked.我试着阅读文档,我用谷歌搜索,我查看了以前工作的代码,我做了所有的事情,但没有任何效果。

Nothing worked until I changed my renderName method from this直到我从这里更改了我的 renderName 方法之前,什么都不起作用

getValues(i){
    console.log(i);
}
renderName(){
    return this.state.names.map(function(name,i){
        return (
            <View>
                <TouchableHighlight
                    key={i}
                    style={styles.viewName}
                    onPress={() => this.getValues(i)}>
                        <Text style={styles.name}>{name}</Text>
                </TouchableHighlight>
            </View>
        )
    })
}

To this对此

getValues(i){
    console.log(i);
}
renderName(){
    return this.state.names.map((name,i) => {
        return (
            <View>
                <TouchableHighlight
                    key={i}
                    style={styles.viewName}
                    onPress={() => this.getValues(i)}>
                        <Text style={styles.name}>{name}</Text>
                </TouchableHighlight>
            </View>
        )
    })
}

The only difference is that I used a fat arrow function this.state.names.map((name,i) => { instead of the normal one this.state.names.map(function(name,i){唯一的区别是我使用了一个箭头函数this.state.names.map((name,i) => {而不是普通的this.state.names.map(function(name,i){

After making that change the results are what I was expecting Clicking a user name output the index associated with the name in the array进行更改后,结果正是我所期望的单击用户名输出与数组中的名称相关联的索引

This may not solve your problem but I'll state it anyway through the following points这可能无法解决您的问题,但无论如何我都会通过以下几点说明

  • I caused my problem by trying to be too familiar with a Library I've basically just started learning.我试图过于熟悉一个我基本上刚刚开始学习的库,从而导致了我的问题。
  • Not every new error should make us panic, that's when we start asking bunch of questions rather than being calm enough to realize that the cause of the error is in front of us.并不是每一个新的错误都应该让我们恐慌,那是我们开始问一堆问题而不是足够冷静地意识到错误的原因就在我们面前的时候。
  • Experimenting with code is a good thing while you are learning but make sure that the code works as the tutorial instructs and that you fully understand it before you start making changes and playing around with it.在学习期间尝试代码是一件好事,但请确保代码按照教程的指示工作,并且在开始进行更改和尝试之前完全理解它。

According to me you might be using Functional Component and in functional component you can call methods directly without using this.根据我的说法,您可能正在使用功能组件,而在功能组件中,您可以直接调用方法而不使用它。 Just add const keyword outside the method and call without this keyword.只需在方法外添加 const 关键字并在没有此关键字的情况下调用。

OnPress takes a function, instead, you're sending an encapsulated function. OnPress 接受一个函数,相反,您发送的是一个封装的函数。

Try this试试这个

onContactPress = (item) =>{
  Alert.alert();        
}    

renderFlatListItem({item}) {
return (
<View style={styles.container}>
  <TouchableOpacity onPress={this.onContactPress(item)} >
    <Image style={styles.rightArrow}
      source={require('../../images/right_arrow.png')}/> 
  </TouchableOpacity>
</View>
)  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native:React Native未定义不是对象(评估&#39;this._lazyCallableModules - React Native : react native undefined is not an object (evaluating 'this._lazyCallableModules React native Undefined不是对象(评估&#39;_react3.default.PropType.shape&#39;) - React native Undefined is not an object(evaluating'_react3.default.PropType.shape') React Native:undefined不是一个对象(评估&#39;r.default.manifest.env&#39;) - React Native: undefined is not an object (evaluating 'r.default.manifest.env') React Native-“未定义不是对象(正在评估&#39;MyApp.navigation.openDrawer&#39;) - React Native - "undefined is not an object (evaluating 'MyApp.navigation.openDrawer') React Native,Android,未定义不是对象(评估&#39;this.props.navigator.push&#39;) - React Native, Android, undefined is not an object (evaluating 'this.props.navigator.push') undefined is not an object (evalating 'this.props.navigation.navigate') error in react native - undefined is not an object (evaluating 'this.props.navigation.navigate') error in react native 未定义不是 object(评估 '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO] - Undefined is not an object (evaluating '_PushTokenManager.default.getDevicePushTokenAsync [REACT NATIVE EXPO] React Native错误:undefined不是对象(评估&#39;_this2.props.navigator.push&#39;) - React Native error: undefined is not an object (evaluating '_this2.props.navigator.push') undefined 不是一个对象(评估&#39;this.props.navigation.navigate&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native React Native:undefined 不是对象(评估“props.navigation.navigate”) - React Native: undefined is not an object (evaluating 'props.navigation.navigate')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM