Code Monkey home page Code Monkey logo

Comments (8)

ShenChang618 avatar ShenChang618 commented on July 29, 2024

@qingant执行到 componentWillUnactivate 生命周期的时候,界面已经被卸载了,所以不能再这里记录位置

from react-keep-alive.

ShenChang618 avatar ShenChang618 commented on July 29, 2024

@qingant 可以考虑在切换页面的事件上记录位置,比如我点击一个按钮会切换到一个页面,那就可以在按钮的点击事件里写

from react-keep-alive.

qingant avatar qingant commented on July 29, 2024

@qingant 可以考虑在切换页面的事件上记录位置,比如我点击一个按钮会切换到一个页面,那就可以在按钮的点击事件里写

切换页面的事件逻辑往往是在别的组件里面,比方list里面装elem,不可能在elem里面记录list的scroll(当然技术上也可以,但抽象上不太好)

在bindLifeCycle修饰的对象里面,是不是可以得到自己的状态呢?比方处于Unactivate的状态

from react-keep-alive.

qingant avatar qingant commented on July 29, 2024

感谢回复,我现在用这样的办法来解决:

   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.

ShenChang618 avatar ShenChang618 commented on July 29, 2024

@qingant Good

from react-keep-alive.

 avatar commented on July 29, 2024

我自己实现一个自动恢复的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.

wl05 avatar wl05 commented on July 29, 2024

@pan-jiaquan 可以,有空研究一下你的代码

from react-keep-alive.

wl05 avatar wl05 commented on July 29, 2024

@pan-jiaquan 我的页面是一个滚动加载的页面,我发现会造成同一次请求会触发两次

from react-keep-alive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.