复制每个组件安装的对象。我如何使其只能运行一次?在反应

时间:2018-10-30 04:20:31

标签: javascript jquery reactjs

我的对象是我创建的一个独立的js文件。

componentDidMount() {
    const node = ReactDOM.findDOMNode(this);
    const widgetBuild = new window.WidgetFormBuilder({
        form: $(node).parents('#dynamic_form_wrapper')
    });
    widgetBuild.initForm();
}

2 个答案:

答案 0 :(得分:0)

在不了解WidgetFormBuilder的情况下很难锻炼。

不过,我建议您采取良好的做法...

componentDidMount() {
  const node = ReactDOM.findDOMNode(this);
  // Assign to the class instance
  this.widgetBuild = new window.WidgetFormBuilder({
    form: $(node).parents('#dynamic_form_wrapper')
  });
  this.widgetBuild.initForm();
}

componentWillUnmount() {
  // Cleanup
  // Check if WidgetFormBuilder has a destroy method or something similar.
  // See https://reactjs.org/docs/react-component.html#componentwillunmount
  this.widgetBuild = null;
}

shouldComponentUpdate() {
  // Stop further re-renders, given you're using the DOM directly this could help prevent a few performance issues
  // See https://reactjs.org/docs/react-component.html#shouldcomponentupdate
  return false;
}

最后,看看the react docs on third party libs

答案 1 :(得分:0)

我已经修复了我刚刚在WidgetFormBuilder中添加了一个.destroy()函数的问题。 :)

WidgetFormBuilder.prototype.destroyBuilder = function () {
const self = this;

const destroyEvents = function () {
    $(self.form).unbind();
};

destroyEvents();

return this;

};