Javascript 如何在react/redux应用程序中重用函数?
我有一个保存页面的链接Javascript 如何在react/redux应用程序中重用函数?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我有一个保存页面的链接 <Link onClick={this.savePDF} button="secondary" > Save as PDF </Link> 这对我来说很有效,但当我尝试这样做的时候 savePDF = (page) => { this.save(page); window.open(`${window.location.pathname}/print`, '_blank'); }
<Link
onClick={this.savePDF}
button="secondary" >
Save as PDF
</Link>
这对我来说很有效,但当我尝试这样做的时候
savePDF = (page) => {
this.save(page);
window.open(`${window.location.pathname}/print`, '_blank');
};
它不会触发this.save(),尽管它会打开打印页面
我如何组织它以减少代码重复 当您像这样调用时,
save
函数将返回一个新函数
this.save(page);
我认为没有必要在这里返回新函数。您可以简单地定义这样的函数,这样就可以了
save = (page) => {
this.props.toggleSaving();
this.props.savePage(page);
this.props.toggleSaving();
};
// And then calling it like this will work
this.save(page);
save
函数在您像这样调用它时返回一个新函数
this.save(page);
我认为没有必要在这里返回新函数。您可以简单地定义这样的函数,这样就可以了
save = (page) => {
this.props.toggleSaving();
this.props.savePage(page);
this.props.toggleSaving();
};
// And then calling it like this will work
this.save(page);
yap-t不起作用,因为我返回了一个新函数。。谢谢…yap-t不起作用,因为我返回了一个新函数。。谢谢