2017-10-16 6 views
1

Deux problèmes, si je lie ma fonction comme ceci:React-native ne peut pas lire la propriété 'bind' undefined

deleteTag = (id) => { 
    console.log(id); 
    id = 0; 
    tabTag.splice(id, 1); 
    --tabSize; 
} 

    componentTag() { 
    return tabTag.map(function(item, id){ 
     return(
     <View key={id} style={styles.componentView}> 
      <Icon name="ios-reorder"></Icon> 
      <Text>{item.name}</Text> 
      <Slider style={styles.sliderBar} maximumValue={3} step={1} /> 
      <TouchableHighlight onPress={() => this.deleteTag.bind(this)}> 
      <Icon name="close-circle"/> 
      </TouchableHighlight> 
     </View> 
    ); 
    }); 
    } 

Mon erreur est 'Impossible de lire la propriété 'bind' undefined'

Else

si je lie ma fonction dans rien constructeur se produit

constructor(props) { 
    this.deleteTag = this.deleteTag.bind(this); 
    } 

deleteTag = (id) => { 
    console.log(id); 
    id = 0; 
    tabTag.splice(id, 1); 
    --tabSize; 
} 

    componentTag() { 
    return tabTag.map(function(item, id){ 
     return(
     <View key={id} style={styles.componentView}> 
      <Icon name="ios-reorder"></Icon> 
      <Text>{item.name}</Text> 
      <Slider style={styles.sliderBar} maximumValue={3} step={1} /> 
      <TouchableHighlight onPress={this.deleteTag}> 
      <Icon name="close-circle"/> 
      </TouchableHighlight> 
     </View> 
    ); 
    }); 
    } 

quelqu'un peut me aider? Merci !

Répondre

3

C'est parce que vous avez oublié de lier this avec carte fonction de rappel et this dans la fonction de rappel ne renvoie pas à réagir contexte de classe, ici:

tabTag.map(function(item, id){ .... })

Utilisation arrow function:

tabTag.map((item, id) => { .... }) 

Maintenant, écrivez le corps avec votre 1ère ou 2ème approche, les deux vont travailler.

+0

Vous êtes mon roi merci;) –

+0

hehe, heureux qu'il a résolu votre problème :) –