我正在对 React 程序进行基本介绍,并且想知道我是否正确理解了虚拟 dom 的差异。
我有这个小应用程序:
import React, { Component } from 'react';
const svgStyle = {
border: '1px solid black'
};
const Circle =({x, y}) => (
<circle cx={x} cy={y} r="5" />
);
class Canvas extends Component {
constructor(props) {
super(props);
this.state = {
circles: []
};
this.addCircle = this.addCircle.bind(this);
}
addCircle(e) {
const {left: rectLeft, top: rectTop} = e.target.getBoundingClientRect();
const {clientX, clientY} = e;
this.setState({
circles: this.state.circles.concat([{x: clientX - rectLeft, y: clientY - rectTop}])
});
}
render() {
var {circles} = this.state;
return (
<svg width="500" height="500" viewBox="0 0 500 500" onClick={this.addCircle} style={svgStyle}>
{
circles.map(circle => (<Circle x={circle.x} y={circle.y} r="5" />))
}
</svg>
);
}
}
export default Canvas;
我的问题是,<circles>
每当我向状态对象添加一个 svg 时,所有的 svg 都会重新渲染吗?或者 React 是否区分了状态中的那些已经存在并且不需要对它们做任何事情的事实。