首页 > 解决方案 > javascript(ReactJS),如何执行函数的引用

问题描述

如果我有一个我想在 a 中使用的函数onClick,比如这个

*使用 ReactJS 和 Semantic-UI

<Table.HeaderCell
  onClick={this.handleSort(items)}
>

其中函数返回函数的引用,例如 this

handleSort = (items) => () => {
    console.log('now')
}

那么,如何在 onClick 事件之外执行此功能?什么不起作用:

componentDidMount() {
    this.handleSort.call(items);
    this.handleSort(items);
}

标签: javascriptreactjs

解决方案


handleSort返回一个函数,您需要使用 调用它(),如下所示:

componentDidMount() {
    this.handleSort(items)();
}

推荐阅读