【问题标题】:undefined is not a function(evaluating........... in react native when clicking on onPressundefined 不是函数(评估...........点击onPress时反应原生
【发布时间】:2018-08-16 17:52:04
【问题描述】:

这是代码,我无法在单击图标标签时调用removeItem 函数。请帮助我,我是 react native 的初学者。我被困了 3 天。

请帮助我以正确的方式调用函数。感谢高级

import React from 'react';
import { StyleSheet, Text, View,TextInput,KeyboardAvoidingView,Dimensions,ScrollView,Alert,TouchableOpacity,Button,TouchableHighlight } from 'react-native';

import Icon from 'react-native-vector-icons/Entypo';

var {height, width} = Dimensions.get('window');

var d = new Date();

export default class App extends React.Component {

    constructor(props){

        super(props); 
        this.state = {
            noteList: [],
            noteText: ''

        }
    }

    addItems(){
        var a = this.state.noteText;
        this.state.noteList.push(a)
        this.setState({
            noteText:''
        })
        console.log(this.state.noteList)
            }

        removeItem(key) {
            console.log('removeItem is working',key);
            }



   render() {

    return ( 

      <KeyboardAvoidingView style={styles.container} behavior="padding" enabled>
        <View style={styles.header}>
            <Text style={{fontSize: 20}}>NOTE APPLICATION</Text>
        </View>

        <View style={styles.body}>
            <ScrollView>
                {this.state.noteList.map(function(value,key){
                return(
                    <View key={key} style={styles.bodyElements} > 
                        <Text>{key}</Text>
                        <Text>{value}</Text>
                        <Text>{d.toDateString()}</Text>
                         <Icon onPress={(key) => this.removeItem(key)} name="cross" color="white" size={40}/>
                    </View>
                )  
                })}
            </ScrollView>

        </View>

        <View style={styles.footer}>
            <TextInput style={{marginTop:10,marginLeft:10}}
            placeholder="Jot down your thoughts before they vanish :)"
            width={width/1.2}
            underlineColorAndroid="transparent"
            onChangeText={(noteText) => this.setState({noteText})}
            value={this.state.noteText}
        />
        <Icon style={{marginTop:15}} name="add-to-list" color="white" size={40} onPress={this.addItems.bind(this)}/>
        </View>
    </KeyboardAvoidingView>
    );
  }
}

【问题讨论】:

    标签: react-native


    【解决方案1】:

    我没有你的数组数据,所以我使用 a,b 值。但是地图功能的问题在这里,您需要将 this 作为 params 传递。签入代码

    import React from 'react';
    
    import { StyleSheet, Text, View, TextInput, KeyboardAvoidingView, Dimensions, ScrollView, Alert, TouchableOpacity, Button, TouchableHighlight } from 'react-native';
    
    //  import Icon from 'react-native-vector-icons/Entypo';
    
    var { height, width } = Dimensions.get('window');
    
    var d = new Date();
    
    export default class App extends React.Component {
    
      constructor(props) {
    
        super(props);
        this.state = {
          noteList: ['a','b'],
          noteText: ''
    
        }
      }
    
      addItems() {
        var a = this.state.noteText;
        this.state.noteList.push(a)
        this.setState({
          noteText: ''
        })
        console.log(this.state.noteList)
      }
    
      removeItem(key) {
        console.warn('removeItem is working');
      }
      render() {
        return (
          <View >
            <View style={styles.header}>
              <Text style={{ fontSize: 20 }}>NOTE APPLICATION</Text>
              <Button title="try" onPress={(key) => this.removeItem()} name="cross"
                size={40} />
    
            </View>
    
            <View style={styles.body}>
    
                    {this.state.noteList.map(function(value,key){
                    return(
                        <View key={key} style={styles.bodyElements} > 
                            <Text>{key}</Text>
                            <Text>{value}</Text>
                            <Text>{d.toDateString()}</Text>
                             <Button title="try" 
                             onPress={() => this.removeItem()} 
                             name="cross"
                              color="white"
                               size={40}/>
                        </View>
                    )  
                    },this)}
    
    
            </View>
    
          </View>
        );
      }
    }
    
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 25,
        textAlign: 'center',
        margin: 10,
      },
      child: {
        fontSize: 18,
        textAlign: 'center',
        margin: 10,
        backgroundColor: 'green',
        height: 100,
        width: 200,
      },
    });
    

    【讨论】:

    • 同样的错误兄弟 undefined is not a function evaluation..........我只是改变 removeItem=(key)=> { console.log('removeItem is working',key) ; } 到你的 sn-p
    • 我希望所有代码都包含在一个类中。正确的 ??或者让我更正
    • 渲染方法在哪里?我无法运行此代码。
    • 请看一下
    • 您是否尝试过对其他组件进行压制除了图标 this.removeItem(key)} name="cross" color="white" size={40}/ > 尝试一次按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多