Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从导入的元素访问父函数_Javascript_Reactjs_React Native_Ecmascript 6 - Fatal编程技术网

Javascript 如何从导入的元素访问父函数

Javascript 如何从导入的元素访问父函数,javascript,reactjs,react-native,ecmascript-6,Javascript,Reactjs,React Native,Ecmascript 6,我在index.js我的react原生项目中有一个列表视图,如下所示 import ResultRow from './resultRow' class ResultList extends Component { constructor() { } updateDate(){ //Some operation } onPressRow() { try { console.log("Selecte

我在
index.js
我的react原生项目中有一个列表视图,如下所示

import ResultRow from './resultRow'

class ResultList extends Component {
    constructor() {

    }

    updateDate(){
        //Some operation
    }

    onPressRow() {
        try {
          console.log("Selected!");

        //Some operation

          this.updateDate(); // Got undefined is not a function

        } catch (error) {      
          console.error(error);
        }
    }

    renderRow(rowData) {
        return (
          <ResultRow
          onPress={this.onPressRow}
            {...rowData} />
        )
      }

    render() {
      return (
              <ListView
                style={[styles.container, { padding: 10, backgroundColor: '#ddd' }]}
                dataSource={this.state.dataSource}
                renderRow={this.renderRow.bind(this)} />
            );
    }

}
如果我从列表视图中选择一行,请按调用的事件。并执行
onPressRow
功能。从
onPressRow
函数中,我调用了另一个函数,该函数在名为“
updateDate
”的同一类中定义。我这样调用
this.updateDate()
但got
未定义不是函数错误

我做错了什么


提前谢谢

您需要
绑定
函数,因为
未引用代码中的适当上下文。您可以使用箭头功能

onPressRow = () => {
        try {
          console.log("Selected!");

        //Some operation

          this.updateDate(); 

        } catch (error) {      
          console.error(error);
        }
   }
绑定函数的另一种方法是在构造函数中设置绑定

constructor() {
   super();
   this.onPressRow = this.onPressRow.bind(this);
}

事实上,您将需要
绑定
任何将使用
引用react类的
上下文的函数。

绑定函数或使用箭头函数<代码>this.onPressRow=this.onPressRow.bind(this)在构造函数中。@AndrewLi谢谢。现已修复:)您的示例不是有效的ECMAScript 6。如果您解释了如何使这个(实验性的)功能正常工作,这对其他人来说将是有用的。
constructor() {
   super();
   this.onPressRow = this.onPressRow.bind(this);
}