reactjs - 防止使用 React Router 参数的每个 URI 重新渲染
问题描述
给定以下路由,如果添加或更改了 URI 查询(即?bar2=foo
),则MySpecialComponent
即使未设置为捕获这些参数,也会重新呈现:
<Route exact path="/foo/:bar" render={props => (<MySpecialComponent/>)}/>
在整个使用过程中,这会导致大量的重新渲染。如何忽略不需要的参数?即在我们关心的更改(/foo/:bar
)之前不触发重新渲染。
解决方案
我发现使用render
onroute
以及自己传递参数是最干净的解决方案(不要用路由器逻辑污染容器):
<Route exact path="/foo/:bar" render={props => (
// Only pass needed props to avoid unnecessary re-render on ANY URI change
<MySpecialComponent bar={props.match.params.bar}/>
)}/>
推荐阅读
- python - 我应该怎么做才能更正此代码?作为输出,我必须获得无效输入,然后是最大值和最小值
- wordpress - Ninja Forms 服务器端验证不起作用
- c# - 具有多个 URL 的 HttpWebResponse 导致错误
- codenameone - 代号一逆向工程师安卓包
- c# - 找不到类型或命名空间名称“硬件”(您是否缺少 using 指令或程序集引用?)
- python - 如何在 python 中实现凯撒密码?
- bash - 为什么从 cli 运行 aws update-service 会导致弹出类似 vim 的窗口,需要一些输入?
- range - 如何在 Tableau 条形图中显示工资范围
- javascript - 在javascript中将onclick事件设置为ac# URL Action?
- javascript - 将地图功能导出到其他文件