未定义不是函数(评估'_this2.AlertButton()')?

时间:2019-05-06 11:31:42

标签: react-native

我为地图中的每个项目创建了touchableopacity。它工作正常,但是当我将功能赋予如下所示的TouchableOpacity时,出现标题上的错误。我该如何解决?

在编写此代码时出现错误。AlertButton()

{section.subcategory.map((item, key) => (        
          <View key={key} style={styles.item}>
            <TouchableOpacity
              onPress={() =>this.AlertButton()}>
            </TouchableOpacity>

            <View style={styles.separator} />
          </View>
        ))}

这是我的提醒功能:

AlertButton() {
     const number = 'TelNo';
  Alert.alert(
      '',
      'Test',
      [
        {text: 'NO'},
        {text: 'Yes', onPress: () => Linking.openURL(`tel:${number}`)},
      ],
    );
  }

此处呈现。我上面的地图函数在renderContent

 render() {
      CONTENT2 =[];
    CONTENT2 = this.state.fromPage1;

    return (
      <View style={styles.container}>

      <View style={{height: this.state.yuksek, width: this.state.genis, justifyContent: 'center', alignItems: 'center',position:'absolute'}}>
        <Text style={{fontWeight: 'bold', color:'#856F6F'}}>Yukarıdaki menü butonlarına tıklayınız.</Text>
      </View>

      <View style={{height: this.state.yuksek2, width: this.state.genis2, justifyContent: 'center', alignItems: 'center'}}>
        <Text style={{fontWeight: 'bold', color:'#856F6F'}}>String</Text>
      </View>
        <ScrollView contentContainerStyle={{}}>
          <Accordion
            activeSections={this.state.activeSections}
            sections={CONTENT2}
            touchableComponent={TouchableOpacity}
            expandMultiple={false}
            renderHeader={this.renderHeader}

            renderContent={this.renderContent}
            duration={400}
            onChange={this.setSections}
          />
        </ScrollView>
        <View style={{height:85, width:85,position:'absolute',bottom:5,right:5}}>
        <TouchableOpacity style={{flex:1}} onPress={() => this.AlertButton()}>
            <ImageBackground source={require('../../image/phoneCall.png')} style={{flex:1}}>
            </ImageBackground>
        </TouchableOpacity>
        </View>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

只需更改以下行

renderContent={this.renderContent}

renderContent={this.renderContent.bind(this)}

因为当您直接使用class方法作为回调时,从组件内部调用

时,它不一定绑定到其类