Code Monkey home page Code Monkey logo

Comments (5)

hackerain avatar hackerain commented on June 2, 2024 1

路由切换是可以的,不过刷新后还是没有进到 history.listen 里面

这个问题应该是因为旧版本的plugin-dva里面建dva时用的history是patch过的,在patch过的history中主动去调用了一次callback,所以刷新浏览器会主动执行一次dispatch,但是新版本的plugin-dva就是用的原生的history,就没这功能了。

dva/packages/dva/src/index.js

image

在3.x umi中的plugin-dva中,建dva app是这么建的:

  app = dva({
    history,
    
    ...(runtimeDva.config || {}),
    // @ts-ignore
    ...(typeof window !== 'undefined' && window.g_useSSR ? { initialState: window.g_initialProps } : {}),
    ...(options || {}),
  });

在这个逻辑里就会去走上面截图中建app的逻辑,将history给patch下。

在4.x umi中的plugin-dva中,建dva app是这么建的:

    app.current = create(
      {
        history,
        ...(runtimeDva.config || {}),
      },
      {
        initialReducer: {},
        setupMiddlewares(middlewares: Function[]) {
          return [...middlewares];
        },
        setupApp(app: IDvaApp) {
          app._history = history;
        },
      },
    );

这里面用的history就是原生的了,没有经过修改的。

patch过的history还修了一个挺严重的bug好像,现在不用这个了,不知道还会不会有问题:umijs/umi#2693

from dva.

hackerain avatar hackerain commented on June 2, 2024

经查排查发现 history 功能是使用的 https://github.com/remix-run/history 这个项目,它是react router依赖的项目,当前已经发布到了5.3.0,history.listen() api已经发生了变化,参见 history,现在history.listen()是这么写的:

// Listen for changes to the current location.
let unlisten = history.listen(({ location, action }) => {
  console.log(action, location.pathname, location.state);
});

在location中是包含pathname变量的:

  • location.pathname - The path of the URL、

而在4.x版本的history中,history.listen()是这么写的:

// Listen for changes to the current location.
const unlisten = history.listen((location, action) => {
  // location is an object like window.location
  console.log(action, location.pathname, location.state);
});

可见函数的参数从 (location, action) 变成了 {location, action},后者是一个析构参数,其实是从一个对象参数中析构出来的两个变量,而前者则是两个参数。可以从最新的5.3.0的history代码中,看到listen()定义中,现在只接受一个参数 update,而update包含2个变量:

listen(listener: Listener): () => void;

export interface Listener {
  (update: Update): void;
}

export interface Update {
  action: Action;
  location: Location;
}

所以当年写dva文档时,也就是umi 3.x的时候,应该还依赖的是4.x的history,但是到umi 4.x,由于其依赖的history到了5.x,listen() api发生了变化,导致listen()的函数没法被触发,所以umi 4.x对应的dva里的subscriptions得这么写才对:

app.model({
  subscriptions: {
    setup({ dispatch, history }) {
      console.log('xxxx1');
      history.listen(({ location, action }) => {
        console.log('xxxx2');
        if (location.pathname === '/users') {
          console.log('xxxx3');
          dispatch({
            type: 'users/fetch',
          });
        }
      });
    },
  },
});

from dva.

waittingbar avatar waittingbar commented on June 2, 2024

路由切换是可以的,不过刷新后还是没有进到 history.listen 里面

from dva.

hackerain avatar hackerain commented on June 2, 2024

同一问题:umijs/umi#9426

from dva.

wangwufan avatar wangwufan commented on June 2, 2024

同样,我在umi3升级umi4后也发现这个问题,dva subscription切换路由能被侦听到,刷新就不行

from dva.

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.