Comments (8)
@qingant执行到 componentWillUnactivate
生命周期的时候,界面已经被卸载了,所以不能再这里记录位置
from react-keep-alive.
@qingant 可以考虑在切换页面的事件上记录位置,比如我点击一个按钮会切换到一个页面,那就可以在按钮的点击事件里写
from react-keep-alive.
@qingant 可以考虑在切换页面的事件上记录位置,比如我点击一个按钮会切换到一个页面,那就可以在按钮的点击事件里写
切换页面的事件逻辑往往是在别的组件里面,比方list里面装elem,不可能在elem里面记录list的scroll(当然技术上也可以,但抽象上不太好)
在bindLifeCycle修饰的对象里面,是不是可以得到自己的状态呢?比方处于Unactivate的状态
from react-keep-alive.
感谢回复,我现在用这样的办法来解决:
componentDidActivate() {
console.log('restore', this.state)
setTimeout(e => {this.refs.scroller.scrollTop = this.state.scrollTop;}, 10);
this.state.activated = true
}
componentWillUnactivate() {
this.state.activated = false
}
onScroll() {
if (this.state.activated) {
this.state.scrollTop = this.refs.scroller && this.refs.scroller.scrollTop
}
}
it works!
from react-keep-alive.
@qingant Good
from react-keep-alive.
我自己实现一个自动恢复的hook,你可以参考一下:
import React, { useState, useRef, useEffect } from 'react';
import { useKeepAliveEffect } from 'react-keep-alive';
function isEqualAndTruthy(
newInputs?: React.DependencyList,
lastInputs?: React.DependencyList
) {
if (!newInputs || !lastInputs) {
return false;
}
if (newInputs.length !== lastInputs.length) {
return false;
}
for (let i = 0; i < newInputs.length; i++) {
if (newInputs[i] !== lastInputs[i]) {
return false;
}
}
return true;
}
export default function useScrollRestoration(deps?: React.DependencyList) {
const [scrollY, setScrollY] = useState<number | null>(null);
useKeepAliveEffect(() => {
let ticking: number | null = null;
const handler = () => {
if (ticking === null) {
ticking = window.requestAnimationFrame(() => {
setScrollY(window.scrollY);
ticking = null;
});
}
};
window.addEventListener('scroll', handler);
return () => {
window.removeEventListener('scroll', handler);
if (ticking !== null) {
window.cancelAnimationFrame(ticking);
}
};
});
const savedDeps = useRef(deps);
const savedCallback = useRef(() => {});
useEffect(() => {
savedCallback.current = () => (savedDeps.current = deps);
});
useKeepAliveEffect(() => {
if (scrollY !== null) {
const y = isEqualAndTruthy(savedDeps.current, deps) ? scrollY : 0;
window.scrollTo(0, y);
}
return () => {
savedCallback.current();
};
});
}
用的时候只需放到对应页面下
export default function PageA() {
// 依赖发生变化时页面回到顶部,不变时还原滚动位置
useScrollRestoration([]);
// ...
}
from react-keep-alive.
@pan-jiaquan 可以,有空研究一下你的代码
from react-keep-alive.
@pan-jiaquan 我的页面是一个滚动加载的页面,我发现会造成同一次请求会触发两次
from react-keep-alive.
Related Issues (20)
- The onClick inside of <KeepAliveProvider /> not work in React 17 HOT 6
- 大佬,我想问下,该插件兼容redux吗?因为详情页面的数据,都是用的同一个字段,每次请求新的详情页面,都会替换之前的redux数据,关于该问题,您这边有解决方法吗?
- 你好,页面包裹KeepAlive之后,state改变,不会重新渲染组件。
- 更新state数据之后,通过props传递给下层组件,下层组件不会重新渲染。 HOT 1
- keep-alive在iframe场景下会失效 HOT 1
- Props aren't updating when wrapped in KeepAlive HOT 1
- onClick event is not working HOT 7
- hooks 中组件离开时,无法获取deactive组件的滚动距离
- 用了以后父子组件的props各种乱套,失效,已经放弃了 HOT 1
- useKeepAliveEffect dosent work HOT 3
- removeCache方法匹配key和name的bug HOT 1
- New implementation HOT 1
- 缓存后的组件,按钮点击等事件无效,这是怎么回事呢 HOT 8
- componentDidActivate Not implemented
- How to force rerender (refresh) the keepalive component (page) ?
- 使用KeepAlive包裹住组件后,里面的点击事件没有效果了 HOT 3
- Try react-fiber-keep-alive
- react-router-dom 6 以上,包裹后报错 HOT 1
- KeepAlive”不能用作 JSX 组件。 其元素类型 "ReactElement<any, any> | Component<IKeepAliveProps, any, any> | null" 不是有效的 JSX 元素。 不能将类型“Component<IKeepAliveProps, any, any>”分配给类型“Element | ElementClass | null”。 不能将类型“Component<IKeepAliveProps, any, any>”分配给类型“ElementClass”。 在这些类型中,"render()" 返回的类型不兼容。
- vite工程下,出现错误
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-keep-alive.