简体   繁体   English

反应生命周期方法,是否为 componentWillReceiveProps

[英]React lifecyle methods, to componentWillReceiveProps or not

I'm building a collapsible OrgChart component with React.我正在用 React 构建一个可折叠的OrgChart组件。

I'm not sure how to use the lifecycle methods to re-render the Edges (SVG path that links 2 nodes), based on user interactions.我不确定如何根据用户交互使用生命周期方法重新渲染边缘(链接 2 个节点的 SVG 路径)。 I would like to know if there is a better approach than using componentWillReceiveProps (or since it's going to be obsolete soon, componentDidUpdate and/or getDerivedStateFromProps instead) for this case对于这种情况,我想知道是否有比使用componentWillReceiveProps更好的方法(或者因为它很快就会过时,改用componentDidUpdate和/或getDerivedStateFromProps

The Edges should only be painted once all the nodes have been painted first.只有先绘制所有节点后,才应绘制边。 However in my code, each Node paints its incoming Edge.但是在我的代码中,每个Node绘制其传入的边缘。 This, because the HTML code that makes the chart collapsible using CSS requires Node and its Edge under the same div :这是因为使用CSS使图表可折叠的 HTML 代码需要Node及其Edge位于同一div

NodeContainer = (props) => 
  <div className='nodeContainer'>
    <Node />
    <Edge />
  </div>

My OrgChart component has 3 main components:我的OrgChart组件有 3 个主要组件:

  • Chart.tsx : receives a graph object props that contains the list of nodes and edges, and uses it to build the chart. Chart.tsx :接收包含节点和边列表的graph对象道具,并使用它来构建图表。 Also receives a few other props such as call back functions for chart or node manipulation (eg: onNodeSelected(id) , onChartMoved() )还接收一些其他道具,例如图表或节点操作的回调函数(例如: onNodeSelected(id) , onChartMoved()
  • Node.tsx : paints both the Node and its incoming svg Edge. Node.tsx :绘制节点及其传入的 svg Edge。
  • Edge.tsx : paints the SVG Edge Edge.tsx : 绘制 SVG 边缘

So far, I was thinking of organizing my code this way:到目前为止,我正在考虑以这种方式组织我的代码:

Chart.tsx图表.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})
  }
}

I would use a simple memoization approach :我会使用一个简单的记忆方法

calculateCoordinates = memoize((nodesLength, graph) => { your recalculation code })

render() {
    const augmentedGraph = this.augmentGraph(this.props.graph.nodes.length, this.props.graph);
    return {buildChart(rootNode, augmentedGraph)};
}

Obviously, there is a lot of detail missing in your question.显然,您的问题中缺少很多细节。 For example, I am missing the connection between augmentedGraph and rootNode .例如,我缺少augmentedGraphrootNode之间的连接。
In general, the memoization approach is used to remove state from the component, in this case, the augmented graph.通常,记忆方法用于从组件中删除状态,在这种情况下,是增强图。
The parameters to the lambda that is passed to memoize are the keys on which you want to decide whether the input data has changed and you need to re-execute your calculation code.传递给memoize的 lambda 参数是您要决定输入数据是否已更改以及需要重新执行计算代码的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM