简体   繁体   English

多个React组件架构

[英]Multiple React components architecture

I am creating a realtime dashboard which consumes an API for the data. 我正在创建一个实时仪表板,它使用数据API。 The application is an isomorphic react app built on top of a node API, I am using the Flux architecture, more specifically the alt implementation . 该应用程序是一个构建在节点API之上的同构反应应用程序,我使用的是Flux架构,更具体地说是alt实现

My question is the dashboard has around 4/5 realtime charts (I am using a React D3 library) , each chart calls an API endpoint. 我的问题是仪表板有大约4/5个实时图表(我使用的是React D3库) ,每个图表都调用一个API端点。 I have a main <Dashboard /> component and inside this each chart component lives. 我有一个主<Dashboard />组件,并且每个图表组件都在其中。 My question is should the <Dashboard /> be responsible for listening for data and passing it though as props or should each graph component be responsible for its own store/action life cycle? 我的问题是<Dashboard />应该负责监听数据并将其作为道具传递,还是应该为每个图形组件负责其自己的存储/动作生命周期? If I pass the data through as props this would this mean the UI would rerender more than is required? 如果我通过道具传递数据,这意味着UI会重新渲染超过需要的数量?

I have yet to find any examples of React/Dashboard/Charting application so if anyone knows of any this would be very useful. 我还没有找到React / Dashboard / Charting应用程序的任何示例,所以如果有人知道任何这将是非常有用的。

It's better to keep the API access right at the top level, and just pass everything down through props. 最好将API访问保持在顶层,并通过道具将所有内容传递下去。 Consider putting the data from the API into a store, and using that to drive the top-level component. 考虑将API中的数据放入商店,并使用它来驱动顶级组件。 The practical advantages of doing it that way: 这样做的实际优势:

  • you now have the freedom to use the same data in more than one place - it's not trapped in the child component 您现在可以自由地在多个位置使用相同的数据 - 它不会被困在子组件中
  • testing is simplified, because you can just pass in some dummy props and confirm that you get the right result 测试是简化的,因为你可以传递一些虚拟道具并确认你得到了正确的结果
  • it's easier to reason about: the child components no longer need to care about the implementation details of where their data came from 它更容易推理:子组件不再需要关心其数据来源的实现细节

Regarding your question about performance: Consider using pure render mixin and only updating the parts of the store that need to change. 关于性能问题:考虑使用纯渲染混合 ,只更新需要更改的商店部分。 That way, your chart components can very quickly tell is anything has changed, and won't re-render if nothing has changed. 这样,您的图表组件可以非常快速地告知任何已更改的内容,并且如果没有任何更改,则不会重新呈现。

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

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