Comments (5)
路由切换是可以的,不过刷新后还是没有进到 history.listen 里面
这个问题应该是因为旧版本的plugin-dva里面建dva时用的history是patch过的,在patch过的history中主动去调用了一次callback,所以刷新浏览器会主动执行一次dispatch,但是新版本的plugin-dva就是用的原生的history,就没这功能了。
dva/packages/dva/src/index.js
在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.
经查排查发现 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.
路由切换是可以的,不过刷新后还是没有进到 history.listen 里面
from dva.
同一问题:umijs/umi#9426
from dva.
同样,我在umi3升级umi4后也发现这个问题,dva subscription切换路由能被侦听到,刷新就不行
from dva.
Related Issues (20)
- dva 的connect和React.memo函数无法连接,代码如下:cont App=()=>{ return <div></div>} export default connect(null,React.memo(App)),这样写页面直接报错 HOT 3
- state直接存贮dom节点,redux页面会很卡顿 HOT 1
- dva注册model HOT 3
- pre-commit hook failed HOT 1
- dva现在不支持react17+版本吗,后续会有支持吗 HOT 4
- 支持 es最新版 HOT 1
- [Feature Request] dispatch可以传入一个数组吗? HOT 1
- 项目不维护了,把你们的备选状态库都拿出来 HOT 5
- "immer": "^8.0.4",版本过低,扫描有安全问题,能不能升级一下到9.0.6 HOT 15
- `dva-core` CJS build relies on a `regeneratorRuntime` global, but it isn't defined HOT 1
- 现在都没人维护了么,react都更新N个版本了,还是19年更新后,路由都到了v6版本了 HOT 7
- 为什么不使用HashRouter ???
- React已经到了v18,请问dva有没有升级的计划?
- dva 的正式版本才 2.4.1 ,怎么 dva-immer 的 peerDependencies 里的 dva 版本已经是 2.5.0 了? HOT 1
- 如何在reducer执行完后用更新后的state来执行effects HOT 1
- react 18 下,dva 的状态更新可能延迟于dispatch.then HOT 2
- dva正式版本目前是2.4.1 不支持hooks(useSelector和useDispatch) 2.6.0正式版本啥时候发布呀 我看2.6.0 后面的版本支持hooks了
- 可以在单个reducer里获取全局state吗
- 有办法兼容react18吗?
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 dva.