Comments (5)
Controlling Rendering Behavior is the key of CacheRoute
.
In render
method of Switch
you can see that there is no any way to do this.
Because Switch
would ONLY render matched route.
Treating unmatched route as null
means that CacheRoute
in Switch
would be removed either.
PS: 英语不太好,可以中文交流~
from react-router-cache-route.
我在react-router的基础上改写了Switch,修改了CacheRoute,可以接受visible属性,目前可以使用CacheSwitch实现Switch Cache.
`
render() {
const { route } = this.context.router;
const { children } = this.props;
const location = this.props.location || route.location;
children = Array.isArray(children) ? children : [children];
let match;
let index = children.findIndex((c) => {
const {
path: pathProp,
exact,
strict,
sensitive,
from
} = c.props;
const path = pathProp || from;
match = matchPath(
location.pathname,
{ path, exact, strict, sensitive },
route.match
);
return match;
})
return children.map((c, i) => {
if (i === index) {
if (c.type === CacheRoute)
return React.cloneElement(c, { location, computedMatch: match, key: i, hidden: false })
return React.cloneElement(c, { location, computedMatch: match })
}
else {
if (c.type === CacheRoute)
return React.cloneElement(c, { hidden: true, key: i })
return null;
}
})
}
另外,CacheComponent如果不能match,子组件就会得到空的match(如果子组件依赖match,有可能出错),所以我将旧match存储起来,如果CacheComponent不能match,就使用旧match.
另外,目前的问题是,CacheRoute不能接受render.
from react-router-cache-route.
适配 Switch
和扩展 CacheRoute
的 render
props 确实是可以做的事情
但其实 CacheSwitch
内部没有必要再去决定组件的 hidden 状态,因为 CacheComponent
已经做了这个事情,再做一次判断显得有些多余,也不符合模块职责单一的**
Switch
的问题在于它直接删除了不匹配的组件,我们可以对它简单动刀,让它面对不匹配的 CacheRoute
时仅如实传递 switch match 结果就好,剩下的事情应该由 CacheRoute
独立完成
另外非 matched 状态下使用旧的 match props 我认为不太妥当,可能会造成一些奇怪的问题,我认为我们应该尽可能遵从原汁原味的 react-router
,不应对这个过程加以干涉,毕竟 Route
children props 作为函数使用的时候,也是有可能出现不匹配状态下 match 为 null 但组件并未销毁,生命周期还在运转的情况,在此状态下造成的后果应由用户自己承担,我们可以在说明中给出提示就够了
我会在近几天尝试加入你需要的功能
from react-router-cache-route.
嗯,谢谢你的回复。CacheSwitch 确实没有必要再去决定组件的 hidden 状态。
另外,CacheComponent保存和使用旧match的问题,因为我们的项目目前已经成型,有很多页面,之前的逻辑是,只有页面match时才会显示,所以页面接收的props.match不可能为null,我们也没有判断match为null的情况。所以为了兼容之前的代码,不得不使用旧的match防止大量error;
from react-router-cache-route.
@tuliprope 更新了如下功能(v1.1.1)
- CacheSwitch
- 非 matched 状态下对应组件不更新,match 值为旧值(其实只是不会更新)
- CacheRoute 的 render、children prop
- CacheRoute 可选缓存时机 [
forward
,back
,always
],通过when
属性控制,默认forward
- CacheRoute 可以给包裹组件添加类名,通过
className
配置 - 兼容 React 16.3 以下版本(componentWillReceiveProps)
from react-router-cache-route.
Related Issues (20)
- 带参数详情页面多份数据缓存如何做? HOT 4
- 带有参数的页面,离开页面的时候useParams会返回undefined,导致无法维持原来的页面组件状态 HOT 3
- 请问下作者如何处理跳转路径完全相同,但是state不同的情况 HOT 2
- 路由里存在中文,路由会缓存2份
- 开启multiple后,带querystring的路由缓存失败
- 请问 在cache-route中如何实现仅在切换路由列表时卸载当前页面组件 谢谢 HOT 1
- `CacheSwitch` re-mount the same component if path changes HOT 1
- item/detail/:id 这种带参数的路由缓存不了,
- 在路由back后,怎么让缓存的组件不重新render呢? HOT 1
- 怎么实现头部Tabs页签的效果?能否提供一些示例? HOT 1
- How to turn off the display:none of the component HOT 5
- 使用CacheRoute缓存路由,出现闪烁
- 【咨询】为什么 Updatable 内部的 Freeze 需要 Delay 1s HOT 2
- CacheSwitch不会触发Suspense的fallback HOT 3
- iframe嵌套状态保留
- 打包部署后报错 HOT 3
- react18+ 安装该库提示报错,最终安装失败
- 切换路由时原页面与悬浮相关的内容都会重新悬浮一遍 HOT 1
- README链接失效 HOT 1
- Uncaught TypeError: Super expression must either be null or a function, not undefined HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-router-cache-route.