undefined不是一个函数(在单击onPress

时间:2018-08-16 17:52:04

标签: react-native

这是我在单击Icon标记时无法调用removeItem函数的代码。请帮助我,我是React Native的初学者。我被困了3天 请以正确的函数调用方式帮助我。感谢进阶

从'react'导入React;

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

从“ react-native-vector-icons / Entypo”导入图标;

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

var d = new Date();

导出默认类App扩展了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>
);

} }

1 个答案:

答案 0 :(得分:2)

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

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,
  },
});