在React.js应用程序中使用freshdesk feedback小部件

时间:2015-08-12 09:43:19

标签: javascript reactjs

我正在尝试在我的React.js应用程序中使用Freshdesk的反馈小部件。我正在尝试初始化并在我的根组件的componentDidMount方法中显示小部件,如下所示

var App = React.createClass({
  componentDidMount: function() {
    FreshWidget.init("", {"queryString": "&widgetType=popup", "utf8": "✓", "widgetType": "popup", "buttonType": "text", "buttonText": "Support", "buttonColor": "white", "buttonBg": "#006063", "alignment": "4", "offset": "235px", "formHeight": "500px", "url": "<myfreshdeskurl>"} );
    FreshWidget.show();
  }
});

未显示窗口小部件,并在控制台中抛出以下错误

Freshdesk Error:  TypeError: Cannot read property 'style' of null
at f (http://assets.freshdesk.com/widget/freshwidget.js:1:4741)
at http://assets.freshdesk.com/widget/freshwidget.js:1:6412
at e (http://assets.freshdesk.com/widget/freshwidget.js:1:38)
at Object.C.show (http://assets.freshdesk.com/widget/freshwidget.js:1:6392)
at React.createClass.componentDidMount (http://localhost:2345/:14673:15)
at CallbackQueue.assign.notifyAll (http://localhost:2345/:102289:22)
at ReactReconcileTransaction.ON_DOM_READY_QUEUEING.close (http://localhost:2345/:115822:26)
at ReactReconcileTransaction.Mixin.closeAll (http://localhost:2345/:119697:25)
at ReactReconcileTransaction.Mixin.perform (http://localhost:2345/:119638:16)
at batchedMountComponentIntoNode (http://localhost:2345/:113776:15)

1 个答案:

答案 0 :(得分:3)

您必须在FreshWidget.init选项对象中传递另一个属性:

FreshWidget.init("", {"loadOnEvent": 'immediate',...});

否则小部件将等待已经发生的window.load。

另外请确保在使用屏幕截图选项时等到html2canvas加载,否则会出现另一个错误:

function showWhenHTML2CanvasIsAvailable() {
    if (window.html2canvas) {
        window.FreshWidget.show();
    } else {
        setTimeout(showWhenHTML2CanvasIsAvailable, 100);
    }
}

showWhenHTML2CanvasIsAvailable();