React 中的第三方 DOM 操作

Chr*_*isM 5 jquery svg dom reactjs

我目前正在将旧版 BackboneJS 应用程序移植到 ReactJS。该应用程序使用VexFlow,即 JavaScript 乐谱渲染引擎。我遇到的主要问题之一是 VexFlow 以与 D3 类似的方式将所有内容本身渲染为 SVG。有很多关于将 D3 与 React 结合的信息,在使用空的 Reactref元素作为我的 VexFlow 渲染的目标的情况下,我遵循似乎是一般做法,这一切都发生在componentDidMount

export default class ScoreComponent extends React.Component {

  constructor(props) {
    super(props);
    // Create a 'ref' object, which allows us to reference the React DOM
    // element we create in the render method.
    this.scoreElem = React.createRef();
    ...
  }

  componentDidMount() {
    var score = this.score;
    var elem = this.scoreElem.current;
    score.setElem(elem).render(); // <- All VexFlow rendering happens here...
    ...
  }

  render() {
    return (
      <div className="score" id={this.props.scoreId} ref={this.scoreElem}></div>
    );
  }

}
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但它让我很不舒服,特别是因为我还必须添加大量 jQuery 代码来处理 SVG 元素上的用户交互(例如,单击和拖动复杂的路径对象),React 不会意识到这些.

所以我的问题是:我是否正走在一条会导致我被烧伤的道路上?我真的很喜欢 React,并且很想和 Backbone 说再见。我能够在一个周末轻松移植大部分 UI 代码。我过去看过 Angular,但它似乎太复杂了,而且自以为是。

Ori*_*ori 2

您正朝着正确的方向前进。当你需要使用外部非 React DOM 库在 React 中渲染内容时,可以这样做:

  1. 在构造函数中创建对 DOM 元素的引用。
  2. 在 上启动插件实例componentDidMount(),并将对插件实例的引用添加为组件实例的属性。这将使您能够从其他方法调用实例的方法。
  3. 对 中的 prop 更改做出反应componentDidUpdate()。使用插件实例的引用来更新它。
  4. 明确componentWillUnmount()插件添加/计划/等的所有内容...例如事件侦听器、超时/间隔、在 React 树外部创建的 DOM 节点、正在进行的 AJAX 调用等...
  5. 在渲染中 - 不要向容器添加任何属性,因此它不会在道具/状态更改时重新渲染。

注意:false在 React 16.3 之前,标准方法是通过返回shouldComponentUpdate()和对 中的 props 更改做出反应来防止 props/state 更改时的重新渲染componentWillReceiveProps()。然而,后者即将被弃用,而前者将来将成为建议而不是严格的命令。


这个(非工作)示例大致基于当前的VexFlow 教程

export default class ScoreComponent extends React.Component {
  constructor(props) {
    super(props);
    // 1. create a ref to a DOM element
    this.scoreElem = React.createRef();
    ...
  }

  componentDidMount() {
    const { size } = this.props;
    const elem = this.scoreElem.current;
    // 2. add a reference to the plugin's instance, so you   
    //    can call the plugin in other lifecycle methods
    this.renderer = new VF.Renderer(elem, VF.Renderer.Backends.SVG)
    renderer.resize(size.w, size.h);
    this.context = renderer.getContext();
    ...
  }  

  componentDidUpdate (prevProps) {
    // 3. if the props effect the plugin
    // do something with the plugin
    // for example:
    const { size } = this.props;
    if(size !== prevProps.size) this.renderer.resize(size.w, size.h);
  }

  componentWillUnmount() {
    // 4. teardown:
    // run VexFlow destroy method if available
    // remove non react event listeners
    // clear timeouts and intervals
    // remove DOM nodes rendered outside of react container
    // cancel ongoing AJAX calls
    // etc...
  }

  render() {
    // 5. use only ref on the returned element, any use of properties/state might rerender the element itself.
    return (
      <div className="score" ref={this.scoreElem}></div>
    );
  }

}
Run Code Online (Sandbox Code Playgroud)