【发布时间】:2018-10-03 18:40:16
【问题描述】:
我正在使用 React 构建一个可折叠的 OrgChart 组件。
我不确定如何根据用户交互使用生命周期方法重新渲染边缘(链接 2 个节点的 SVG 路径)。我想知道在这种情况下是否有比使用componentWillReceiveProps 更好的方法(或者因为它很快就会过时,componentDidUpdate 和/或getDerivedStateFromProps)
只有在首先绘制所有节点后才能绘制边缘。但是在我的代码中,每个Node 都会绘制其传入的边缘。这是因为使用CSS 使图表可折叠的HTML 代码需要Node 及其Edge 在同一div 下:
NodeContainer = (props) =>
<div className='nodeContainer'>
<Node />
<Edge />
</div>
我的OrgChart 组件有 3 个主要组件:
-
Chart.tsx:接收包含节点和边列表的graph对象道具,并使用它来构建图表。还接收一些其他道具,例如用于图表或节点操作的回调函数(例如:onNodeSelected(id)、onChartMoved()) -
Node.tsx:绘制节点及其传入的 svg 边缘。 -
Edge.tsx:绘制 SVG 边缘
到目前为止,我一直在考虑这样组织我的代码:
Chart.tsx
// augmentedGraph will have extra data for each node and edge.
// For example: the node coordinates, isCollapsed. The coordinates are purely a UI concern, hence why my incoming graph props should not contain the info.
class Chart extends Component {
state = { augmentedGraph: this.props.graph }
componentDidUpdate(prevProps, prevState) {
if (this.props.graph.nodes.length !== prevProps.graph.nodes.length
|| this.state.augmentedGraph !== prevState.augmentedGraph){
// clone this.props.graph into this.state.augmentedGraph
// and re-calculates all edges and nodes coordinates to add those properties to this.state.augmentedGraph
...
}
}
// Will send callback function props to each Node (onNodeMoved, etc.), that will fire below method
userModifiedChartHandler() {
// re-calculates all edges and nodes coordinates and updates this.state.augmentedGraph
}
// recursively builds the chart html structure with Nodes and Edges.
buildChart(node) {
return ...
}
render() {
const rootNode = this.state.augmentedGraph.nodes.root
const chart = buildChart(rootNode)
return ({chart})
}
}
【问题讨论】:
-
什么是
buildChart? -
使用生命周期方法而不是计算
render方法中的所有内容,您的目标是什么?表现?如果是这样,你不能像 React 文档中描述的那样使用 memoization 吗? -
@enguerran
buildChart是一个递归创建构建图表的 JSX 代码的函数。我没有在这里代表它,因为它与我的问题无关。 -
@DanielHilgarth 我的
Chart组件接收一个graph道具,其中包含node和edge的数组,它们没有任何坐标数据。graph道具会触发重新渲染的唯一时间是用户更改其结构(删除、创建、移动节点)时。为了管理其他用户交互(例如折叠、移动图表、缩放等),我需要在Chart.tsx的state中保留每个节点和边的坐标。因此,我需要使用componentDidUpdate生命周期方法来检查用户是否做了某事。因此,这不仅是为了性能,而且似乎只是为了让我的代码能够正常工作。
标签: javascript reactjs