我目前正在将旧版 BackboneJS 应用程序移植到 ReactJS。该应用程序使用 VexFlow ,JavaScript 乐谱渲染引擎。我遇到的主要问题之一是 VexFlow 以与 D3 类似的方式将所有内容本身渲染为 SVG。有很多关于将 D3 与 React 结合的信息,我遵循在这种情况下使用空 React ref
元素作为我的 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>
);
}
}
尽管这可行,但它让我相当不舒服,特别是因为我还必须添加大量 jQuery 代码来处理 SVG 元素上的用户交互(例如,单击并拖动复杂的路径对象),而 React 不会这样做请注意。
所以我的问题是:我是否正在走一条会导致我被烧伤的道路?我真的很喜欢 React,并且渴望告别 Backbone。我能够在一个周末轻松移植大部分 UI 代码。我过去曾研究过 Angular,但它似乎过于复杂且固执己见。
最佳答案
您正朝着正确的方向前进。当您需要使用外部非 React DOM 库在 React 中渲染内容时,可以这样做:
- 在构造函数中创建对 DOM 元素的引用。
- 在
componentDidMount()
上启动插件实例,并将对插件实例的引用添加为组件实例的属性。这将使您能够从其他方法调用实例的方法。 - 对
componentDidUpdate()
中的 Prop 更改使用react。使用插件实例的引用来更新它。 - 在
componentWillUnmount()
清除插件添加/计划/等的所有内容...例如事件监听器、超时/间隔、在 React 树外部创建的 DOM 节点、正在进行的 AJAX 调用等... - 在渲染中 - 不要向容器添加任何属性,因此它不会在 props/state 更改时重新渲染。
注意:在 React 16.3 之前,标准方法是通过在 shouldComponentUpdate()
中返回 false
来防止 props/state 更改时重新渲染,并且对 componentWillReceiveProps()
中的 props 更改使用react。然而,后者即将被弃用,而前者将来将成为建议而不是严格的命令。
这个(无效)示例大致基于当前的 VexFlow tutorial :
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>
);
}
}
关于jquery - React 中的第三方 DOM 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50240053/