首页 > 解决方案 > 无法在 react-native 中调用父方法

问题描述

我需要调用方法 nameList()。它是父方法。但这对我不起作用。我的代码片段如下。

以下是我的代码片段参考,任何答案都应该受到赞赏。我需要对此进行澄清

nameList = refName => () => {
if(!nameList){
   get call
 }
else(){
   update call
 }
}

renderNamesRight(selectedName) {
    if (selectedNames.length > 0 ) {
      return(
        this.renderCenterNames()
      )
  }
}

renderCenterNames(){
    if (!this.state.pressedCenterNames) {
      return(
        <TouchableOpacity onPress = {()=> this.validateInputFields()} activeOpacity = { .5 }>
          <Text style = {styles.names}>CENTER NAMES</Text>
        </TouchableOpacity>
      )
    } else {
      return(
          <Text style = {styles.names}>RIGHT  NAMES</Text>
      )
    }
  }


validateInputFields() {
     const { validation} = this.state;

     if (this.state.name) {
       alert("validation")
       validation.isFormValid = true;
       this.setState({validation: validation});
       this.nameList("name")
    } else {

      validation.isFormValid = false;

      this.setState({validation: validation});
    }
  }

标签: react-native

解决方案


这不是您定义函数的方式。如果你需要一个没有参数的函数,你必须像这样定义FunctionName = () => {}或使用参数FunctionName = (parameters) => {}

如果函数中只有一个参数,您也可以 像这样在没有()的情况下声明它FunctionName = parameter => {}

替换您的方法定义

nameList = refName => () => {

有了这个

 nameList = (refName) => {

推荐阅读