首页 > 解决方案 > 如何使用参数调用返回的函数?Javascript

问题描述

所以我在 react 中做了一个编辑器,并添加了一些额外的工具,所以我需要根据所选项目更改工具栏。

在此方法只有一个工具栏之前:

bottomToolbar.updateToolbar( nodeattrs.align,
                                                 nodeattrs.fontFamily,
                                                 nodeattrs.fontSize,
                                                 nodeattrs.fill,
                                                 nodeattrs.stroke,
                                                 nodeattrs.strokeWidth )

但是新的工具栏有不同的参数,它只需要 4 个参数。

没有我的新工具栏会根据其状态呈现正确的工具栏,我希望类方法 updateToolbar 根据正在呈现的工具栏进行更改,因此我可以跳过检查所选工具栏的 if 评估,我不想每次调用 updateToolbar 时都检查它。

switch(this.state.selectedToolbar){
        case "SelectorAndText":
          return <TextBottomToolbar 
                    ref={this.bottomToolbarRef}

                    fontSizeUpdater    = {this.fontSizeUpdater}
                    fontColorUpdater   = {this.fontColorUpdater}
                    strokeColorUpdater = {this.strokeColorUpdater}
                    strokeSizeUpdater  = {this.strokeSizeUpdater}
                    fontFamilyUpdater  = {this.fontFamilyUpdater}
                    alignmentUpdater   = {this.alignmentUpdater}
                 />
        break
        case "FreeLine":
            return <LineBottomToolbar 
                    ref={this.bottomToolbarRef}

                    strokeColorUpdater={this.updateStrokeColor.bind(this)}
                    strokeSizeUpdater={this.updateStrokeSize.bind(this)}
                    shadowColorUpdater={this.updateShadowColor.bind(this)}
                    shadowSizeUpdater={this.updateShadowSize.bind(this)}
                   />
        break

正如你所看到的,我有一个对应工具栏的引用,所以我想用伪代码调用该方法,如下所示:

updateToolbar(){
    //return selectedToolbarRef.current.updateToolbar
}

因此,当我在 bottomtoolbar.updateToolbar(bla, bla, bla) 或 .updateToolbar(bla, bla) 外部调用时,它使用返回的函数

编辑:一种方法是

bottomToolbar.updateToolbar()(bla,bla) but the function that updateToolbar returns it's a method that uses **this**, if I call it like that it's undefined

*在评论中解决

标签: javascriptreactjs

解决方案


推荐阅读