简体   繁体   English

React:避免在路由上重新渲染相同的组件

[英]React: Avoiding Re-rendering Same Component on Routing

I have a screen with 10 charts (using recharts.org) -- when clicking on a chart I want to switch the route from http://localhost:3000/charts to http://localhost:3000/charts/first and simply have the first chart shown full screen, but without it being re-rendered since no data will change whatsoever.我有一个带有 10 个图表的屏幕(使用 recharts.org)——单击图表时,我想将路由从http://localhost:3000/charts切换到http://localhost:3000/charts/first并且简单地让第一个图表全屏显示,但没有重新渲染,因为任何数据都不会改变。

Any advice on how I may achieve this?关于我如何实现这一目标的任何建议?

The only way to do that (at least in my previous experiences) would be to have the chart on the same level as the Route (with some conditionals...), if you don't need it on top you can change the route place ... the thing is that it doesn't give much flexibility for grid/flex but it works :P (in my case I had the selected graph always on top so it was perfect fit, you may try some conditional styling based on the location but I am afraid it would rerender)做到这一点的唯一方法(至少在我以前的经验中)是让图表与路线处于同一级别(有一些条件......),如果您不需要它在顶部,您可以更改路线地方...问题是它没有给 grid/flex 提供太多的灵活性,但它可以工作:P(在我的情况下,我选择的图形总是在顶部,所以它非常适合,你可以尝试一些基于条件的样式位置,但我担心它会重新渲染)

<Route  path="/charts" >
 {history.pathname === '/charts/first' && <Chart data ..../>}
 {history.pathname === '/charts/second' && <Chart data ..../>}
 <Route  path="/charts/first" >
  <h1> Up you have the unRendered graph </h1>
</Route>
</Route>

* *

Chart is a wrapper of any rechart graph(bar, pie ...) and has the REACT MEMO IMPLEMENTED Chart 是任何 rechart 图(条形图、饼图...)的包装器,并具有 REACT MEMO IMPLEMENTED

* *

I spent days with this issue if you need more help just comment here我花了几天时间解决这个问题,如果您需要更多帮助,请在此处发表评论

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

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